<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>3.2 长宽转换 | 商业数据分析师-R 语言数据处理</title>
  <meta name="description" content="商业数据分析师的 R 语言学习笔记,主要集中在数据清洗相关包的介绍，从数据导入(本地数据 数据库)到数据清洗，到固定报表输出的自动化流程。" />
  <meta name="generator" content="bookdown 0.22 and GitBook 2.6.7" />

  <meta property="og:title" content="3.2 长宽转换 | 商业数据分析师-R 语言数据处理" />
  <meta property="og:type" content="book" />
  
  
  <meta property="og:description" content="商业数据分析师的 R 语言学习笔记,主要集中在数据清洗相关包的介绍，从数据导入(本地数据 数据库)到数据清洗，到固定报表输出的自动化流程。" />
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="3.2 长宽转换 | 商业数据分析师-R 语言数据处理" />
  
  <meta name="twitter:description" content="商业数据分析师的 R 语言学习笔记,主要集中在数据清洗相关包的介绍，从数据导入(本地数据 数据库)到数据清洗，到固定报表输出的自动化流程。" />
  

<meta name="author" content="Yufei Zhong" />


<meta name="date" content="2021-06-23" />

  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="tidyr-useage.html"/>
<link rel="next" href="tidyr-rectangle.html"/>
<script src="libs/header-attrs-2.8/header-attrs.js"></script>
<script src="libs/jquery-3.5.1/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />









<link href="libs/anchor-sections-1.0.1/anchor-sections.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.0.1/anchor-sections.js"></script>
<link href="libs/tabwid-1.0.0/tabwid.css" rel="stylesheet" />
<script src="libs/htmlwidgets-1.5.3/htmlwidgets.js"></script>
<link href="libs/datatables-css-0.0.0/datatables-crosstalk.css" rel="stylesheet" />
<script src="libs/datatables-binding-0.18/datatables.js"></script>
<link href="libs/dt-core-1.10.20/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="libs/dt-core-1.10.20/css/jquery.dataTables.extra.css" rel="stylesheet" />
<script src="libs/dt-core-1.10.20/js/jquery.dataTables.min.js"></script>
<link href="libs/crosstalk-1.1.1/css/crosstalk.css" rel="stylesheet" />
<script src="libs/crosstalk-1.1.1/js/crosstalk.min.js"></script>


<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>


<link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">R学习笔记</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>欢迎</a>
<ul>
<li class="chapter" data-level="" data-path="abstract.html"><a href="abstract.html"><i class="fa fa-check"></i>内容概要</a></li>
<li class="chapter" data-level="" data-path="sec-licenses.html"><a href="sec-licenses.html"><i class="fa fa-check"></i>授权说明</a></li>
<li class="chapter" data-level="" data-path="session.html"><a href="session.html"><i class="fa fa-check"></i>运行信息</a></li>
<li class="chapter" data-level="" data-path="author.html"><a href="author.html"><i class="fa fa-check"></i>关于本人</a></li>
</ul></li>
<li class="chapter" data-level="1" data-path="data-read-write-description.html"><a href="data-read-write-description.html"><i class="fa fa-check"></i><b>1</b> 数据导入导出</a>
<ul>
<li class="chapter" data-level="1.1" data-path="readxl-description.html"><a href="readxl-description.html"><i class="fa fa-check"></i><b>1.1</b> readxl</a>
<ul>
<li class="chapter" data-level="1.1.1" data-path="readxl-description.html"><a href="readxl-description.html#readxl:install"><i class="fa fa-check"></i><b>1.1.1</b> 安装</a></li>
<li class="chapter" data-level="1.1.2" data-path="readxl-description.html"><a href="readxl-description.html#readxl:usage"><i class="fa fa-check"></i><b>1.1.2</b> 用法</a></li>
<li class="chapter" data-level="1.1.3" data-path="readxl-description.html"><a href="readxl-description.html#batch:read"><i class="fa fa-check"></i><b>1.1.3</b> 批量读取</a></li>
<li class="chapter" data-level="1.1.4" data-path="readxl-description.html"><a href="readxl-description.html#batch:write"><i class="fa fa-check"></i><b>1.1.4</b> 批量输出</a></li>
</ul></li>
<li class="chapter" data-level="1.2" data-path="writexl.html"><a href="writexl.html"><i class="fa fa-check"></i><b>1.2</b> writexl</a>
<ul>
<li class="chapter" data-level="1.2.1" data-path="writexl.html"><a href="writexl.html#writexl:usage"><i class="fa fa-check"></i><b>1.2.1</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="openxlsx-description.html"><a href="openxlsx-description.html"><i class="fa fa-check"></i><b>1.3</b> openxlsx</a>
<ul>
<li class="chapter" data-level="1.3.1" data-path="openxlsx-description.html"><a href="openxlsx-description.html#openxlsx:install"><i class="fa fa-check"></i><b>1.3.1</b> 安装</a></li>
<li class="chapter" data-level="1.3.2" data-path="openxlsx-description.html"><a href="openxlsx-description.html#openxlsx:functions"><i class="fa fa-check"></i><b>1.3.2</b> 基础功能</a></li>
<li class="chapter" data-level="1.3.3" data-path="openxlsx-description.html"><a href="openxlsx-description.html#定制格式输出"><i class="fa fa-check"></i><b>1.3.3</b> 定制格式输出</a></li>
<li class="chapter" data-level="1.3.4" data-path="openxlsx-description.html"><a href="openxlsx-description.html#函数参数"><i class="fa fa-check"></i><b>1.3.4</b> 函数参数</a></li>
<li class="chapter" data-level="1.3.5" data-path="openxlsx-description.html"><a href="openxlsx-description.html#总结"><i class="fa fa-check"></i><b>1.3.5</b> 总结</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="readr-package.html"><a href="readr-package.html"><i class="fa fa-check"></i><b>1.4</b> readr</a>
<ul>
<li class="chapter" data-level="1.4.1" data-path="readr-package.html"><a href="readr-package.html#readr:install"><i class="fa fa-check"></i><b>1.4.1</b> 安装</a></li>
<li class="chapter" data-level="1.4.2" data-path="readr-package.html"><a href="readr-package.html#readr:usage"><i class="fa fa-check"></i><b>1.4.2</b> 用法</a></li>
<li class="chapter" data-level="1.4.3" data-path="readr-package.html"><a href="readr-package.html#readr:write-function"><i class="fa fa-check"></i><b>1.4.3</b> 导出功能</a></li>
<li class="chapter" data-level="1.4.4" data-path="readr-package.html"><a href="readr-package.html#总结-1"><i class="fa fa-check"></i><b>1.4.4</b> 总结</a></li>
</ul></li>
<li class="chapter" data-level="1.5" data-path="data-vroom.html"><a href="data-vroom.html"><i class="fa fa-check"></i><b>1.5</b> vroom</a>
<ul>
<li class="chapter" data-level="1.5.1" data-path="data-vroom.html"><a href="data-vroom.html#安装"><i class="fa fa-check"></i><b>1.5.1</b> 安装</a></li>
<li class="chapter" data-level="1.5.2" data-path="data-vroom.html"><a href="data-vroom.html#用法"><i class="fa fa-check"></i><b>1.5.2</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="1.6" data-path="data-rstudio-addins.html"><a href="data-rstudio-addins.html"><i class="fa fa-check"></i><b>1.6</b> Rstudio导入</a>
<ul>
<li class="chapter" data-level="1.6.1" data-path="data-rstudio-addins.html"><a href="data-rstudio-addins.html#rstudio-工具栏导入"><i class="fa fa-check"></i><b>1.6.1</b> rstudio 工具栏导入</a></li>
<li class="chapter" data-level="1.6.2" data-path="data-rstudio-addins.html"><a href="data-rstudio-addins.html#插件导入"><i class="fa fa-check"></i><b>1.6.2</b> 插件导入</a></li>
</ul></li>
<li class="chapter" data-level="1.7" data-path="data-file-path.html"><a href="data-file-path.html"><i class="fa fa-check"></i><b>1.7</b> 文件路径</a>
<ul>
<li class="chapter" data-level="1.7.1" data-path="data-file-path.html"><a href="data-file-path.html#指定路径"><i class="fa fa-check"></i><b>1.7.1</b> 指定路径</a></li>
<li class="chapter" data-level="1.7.2" data-path="data-file-path.html"><a href="data-file-path.html#默认路径"><i class="fa fa-check"></i><b>1.7.2</b> 默认路径</a></li>
</ul></li>
<li class="chapter" data-level="1.8" data-path="data-expand.html"><a href="data-expand.html"><i class="fa fa-check"></i><b>1.8</b> 拓展</a>
<ul>
<li class="chapter" data-level="1.8.1" data-path="data-expand.html"><a href="data-expand.html#tidyxl:package"><i class="fa fa-check"></i><b>1.8.1</b> tidyxl</a></li>
<li class="chapter" data-level="1.8.2" data-path="data-expand.html"><a href="data-expand.html#参考资料"><i class="fa fa-check"></i><b>1.8.2</b> 参考资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="2" data-path="Data-Manipulation-dplyr.html"><a href="Data-Manipulation-dplyr.html"><i class="fa fa-check"></i><b>2</b> 数据处理之-dplyr</a>
<ul>
<li class="chapter" data-level="2.1" data-path="dplyr-description.html"><a href="dplyr-description.html"><i class="fa fa-check"></i><b>2.1</b> 前言</a>
<ul>
<li class="chapter" data-level="2.1.1" data-path="dplyr-description.html"><a href="dplyr-description.html#dplyr:install-package"><i class="fa fa-check"></i><b>2.1.1</b> 安装</a></li>
<li class="chapter" data-level="2.1.2" data-path="dplyr-description.html"><a href="dplyr-description.html#dplyr:difference-of-sql"><i class="fa fa-check"></i><b>2.1.2</b> Excel and Sql 类比</a></li>
<li class="chapter" data-level="2.1.3" data-path="dplyr-description.html"><a href="dplyr-description.html#常见问题"><i class="fa fa-check"></i><b>2.1.3</b> 常见问题</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="dplyr-usage.html"><a href="dplyr-usage.html"><i class="fa fa-check"></i><b>2.2</b> 基础用法</a>
<ul>
<li class="chapter" data-level="2.2.1" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-filter"><i class="fa fa-check"></i><b>2.2.1</b> filter</a></li>
<li class="chapter" data-level="2.2.2" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-select"><i class="fa fa-check"></i><b>2.2.2</b> select</a></li>
<li class="chapter" data-level="2.2.3" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-rename"><i class="fa fa-check"></i><b>2.2.3</b> rename</a></li>
<li class="chapter" data-level="2.2.4" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-relocate"><i class="fa fa-check"></i><b>2.2.4</b> relocate</a></li>
<li class="chapter" data-level="2.2.5" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-mutate"><i class="fa fa-check"></i><b>2.2.5</b> mutate</a></li>
<li class="chapter" data-level="2.2.6" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-arrange"><i class="fa fa-check"></i><b>2.2.6</b> arrange</a></li>
<li class="chapter" data-level="2.2.7" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-summarise"><i class="fa fa-check"></i><b>2.2.7</b> summarise</a></li>
<li class="chapter" data-level="2.2.8" data-path="dplyr-usage.html"><a href="dplyr-usage.html#dplyr-groupby"><i class="fa fa-check"></i><b>2.2.8</b> group_by</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="dplyr-functions.html"><a href="dplyr-functions.html"><i class="fa fa-check"></i><b>2.3</b> 常用函数</a>
<ul>
<li class="chapter" data-level="2.3.1" data-path="dplyr-functions.html"><a href="dplyr-functions.html#条件判断"><i class="fa fa-check"></i><b>2.3.1</b> 条件判断</a></li>
<li class="chapter" data-level="2.3.2" data-path="dplyr-functions.html"><a href="dplyr-functions.html#case_when"><i class="fa fa-check"></i><b>2.3.2</b> case_when</a></li>
<li class="chapter" data-level="2.3.3" data-path="dplyr-functions.html"><a href="dplyr-functions.html#计数函数"><i class="fa fa-check"></i><b>2.3.3</b> 计数函数</a></li>
<li class="chapter" data-level="2.3.4" data-path="dplyr-functions.html"><a href="dplyr-functions.html#排序函数"><i class="fa fa-check"></i><b>2.3.4</b> 排序函数</a></li>
<li class="chapter" data-level="2.3.5" data-path="dplyr-functions.html"><a href="dplyr-functions.html#提取向量"><i class="fa fa-check"></i><b>2.3.5</b> 提取向量</a></li>
<li class="chapter" data-level="2.3.6" data-path="dplyr-functions.html"><a href="dplyr-functions.html#dplyr:slice"><i class="fa fa-check"></i><b>2.3.6</b> slice 系列</a></li>
<li class="chapter" data-level="2.3.7" data-path="dplyr-functions.html"><a href="dplyr-functions.html#group-系列"><i class="fa fa-check"></i><b>2.3.7</b> group 系列</a></li>
<li class="chapter" data-level="2.3.8" data-path="dplyr-functions.html"><a href="dplyr-functions.html#其它函数"><i class="fa fa-check"></i><b>2.3.8</b> 其它函数</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="dplyr-merge-two-table.html"><a href="dplyr-merge-two-table.html"><i class="fa fa-check"></i><b>2.4</b> 表关联</a>
<ul>
<li class="chapter" data-level="2.4.1" data-path="dplyr-merge-two-table.html"><a href="dplyr-merge-two-table.html#dplyr:merge-table-usage"><i class="fa fa-check"></i><b>2.4.1</b> 两表关联</a></li>
<li class="chapter" data-level="2.4.2" data-path="dplyr-merge-two-table.html"><a href="dplyr-merge-two-table.html#dplyr:merge-multiple-table"><i class="fa fa-check"></i><b>2.4.2</b> 多表操作</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html"><i class="fa fa-check"></i><b>2.5</b> 分组操作</a>
<ul>
<li class="chapter" data-level="2.5.1" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#添加分组"><i class="fa fa-check"></i><b>2.5.1</b> 添加分组</a></li>
<li class="chapter" data-level="2.5.2" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#查看分组"><i class="fa fa-check"></i><b>2.5.2</b> 查看分组</a></li>
<li class="chapter" data-level="2.5.3" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#更改和添加分组变量"><i class="fa fa-check"></i><b>2.5.3</b> 更改和添加分组变量</a></li>
<li class="chapter" data-level="2.5.4" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#删除分组变量"><i class="fa fa-check"></i><b>2.5.4</b> 删除分组变量</a></li>
<li class="chapter" data-level="2.5.5" data-path="dplyr-groupby-manipulation.html"><a href="dplyr-groupby-manipulation.html#动词影响"><i class="fa fa-check"></i><b>2.5.5</b> 动词影响</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html"><i class="fa fa-check"></i><b>2.6</b> 列操作</a>
<ul>
<li class="chapter" data-level="2.6.1" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html#dplyr-column-manipulation-basic-usage"><i class="fa fa-check"></i><b>2.6.1</b> 基础用法</a></li>
<li class="chapter" data-level="2.6.2" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html#多种函数功能"><i class="fa fa-check"></i><b>2.6.2</b> 多种函数功能</a></li>
<li class="chapter" data-level="2.6.3" data-path="dplyr-column-manipulation.html"><a href="dplyr-column-manipulation.html#当前列"><i class="fa fa-check"></i><b>2.6.3</b> 当前列</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="dplyr-row-manipulation.html"><a href="dplyr-row-manipulation.html"><i class="fa fa-check"></i><b>2.7</b> 行操作</a>
<ul>
<li class="chapter" data-level="2.7.1" data-path="dplyr-row-manipulation.html"><a href="dplyr-row-manipulation.html#比较差异"><i class="fa fa-check"></i><b>2.7.1</b> 比较差异</a></li>
<li class="chapter" data-level="2.7.2" data-path="dplyr-row-manipulation.html"><a href="dplyr-row-manipulation.html#常用案例"><i class="fa fa-check"></i><b>2.7.2</b> 常用案例</a></li>
</ul></li>
<li class="chapter" data-level="2.8" data-path="dplyr-programming.html"><a href="dplyr-programming.html"><i class="fa fa-check"></i><b>2.8</b> <code>dplyr</code>编程</a>
<ul>
<li class="chapter" data-level="2.8.1" data-path="dplyr-programming.html"><a href="dplyr-programming.html#案例"><i class="fa fa-check"></i><b>2.8.1</b> 案例</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="dplyr-reference-material.html"><a href="dplyr-reference-material.html"><i class="fa fa-check"></i><b>2.9</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="Data-tidyr-package.html"><a href="Data-tidyr-package.html"><i class="fa fa-check"></i><b>3</b> 整洁数据之 tidyr</a>
<ul>
<li class="chapter" data-level="3.1" data-path="tidyr-useage.html"><a href="tidyr-useage.html"><i class="fa fa-check"></i><b>3.1</b> tidyr 介绍</a>
<ul>
<li class="chapter" data-level="3.1.1" data-path="tidyr-useage.html"><a href="tidyr-useage.html#tidyr:install"><i class="fa fa-check"></i><b>3.1.1</b> 安装</a></li>
<li class="chapter" data-level="3.1.2" data-path="tidyr-useage.html"><a href="tidyr-useage.html#tidyr:functions"><i class="fa fa-check"></i><b>3.1.2</b> 主要功能</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="pivoting.html"><a href="pivoting.html"><i class="fa fa-check"></i><b>3.2</b> 长宽转换</a>
<ul>
<li class="chapter" data-level="3.2.1" data-path="pivoting.html"><a href="pivoting.html#宽转长"><i class="fa fa-check"></i><b>3.2.1</b> 宽转长</a></li>
<li class="chapter" data-level="3.2.2" data-path="pivoting.html"><a href="pivoting.html#用法-1"><i class="fa fa-check"></i><b>3.2.2</b> 用法</a></li>
<li class="chapter" data-level="3.2.3" data-path="pivoting.html"><a href="pivoting.html#长转宽"><i class="fa fa-check"></i><b>3.2.3</b> 长转宽</a></li>
<li class="chapter" data-level="3.2.4" data-path="pivoting.html"><a href="pivoting.html#用法-2"><i class="fa fa-check"></i><b>3.2.4</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="tidyr-rectangle.html"><a href="tidyr-rectangle.html"><i class="fa fa-check"></i><b>3.3</b> 矩形数据</a></li>
<li class="chapter" data-level="3.4" data-path="tidyr-nest-data.html"><a href="tidyr-nest-data.html"><i class="fa fa-check"></i><b>3.4</b> 嵌套数据</a>
<ul>
<li class="chapter" data-level="3.4.1" data-path="tidyr-nest-data.html"><a href="tidyr-nest-data.html#基础"><i class="fa fa-check"></i><b>3.4.1</b> 基础</a></li>
<li class="chapter" data-level="3.4.2" data-path="tidyr-nest-data.html"><a href="tidyr-nest-data.html#嵌套数据和模型"><i class="fa fa-check"></i><b>3.4.2</b> 嵌套数据和模型</a></li>
</ul></li>
<li class="chapter" data-level="3.5" data-path="character-vector.html"><a href="character-vector.html"><i class="fa fa-check"></i><b>3.5</b> 字符处理</a>
<ul>
<li class="chapter" data-level="3.5.1" data-path="character-vector.html"><a href="character-vector.html#新列提取"><i class="fa fa-check"></i><b>3.5.1</b> 新列提取</a></li>
<li class="chapter" data-level="3.5.2" data-path="character-vector.html"><a href="character-vector.html#拆分"><i class="fa fa-check"></i><b>3.5.2</b> 拆分</a></li>
<li class="chapter" data-level="3.5.3" data-path="character-vector.html"><a href="character-vector.html#列拆分行"><i class="fa fa-check"></i><b>3.5.3</b> 列拆分行</a></li>
<li class="chapter" data-level="3.5.4" data-path="character-vector.html"><a href="character-vector.html#合并多列"><i class="fa fa-check"></i><b>3.5.4</b> 合并多列</a></li>
</ul></li>
<li class="chapter" data-level="3.6" data-path="tidyr-expand.html"><a href="tidyr-expand.html"><i class="fa fa-check"></i><b>3.6</b> 扩展数据框</a></li>
<li class="chapter" data-level="3.7" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html"><i class="fa fa-check"></i><b>3.7</b> 缺失值</a>
<ul>
<li class="chapter" data-level="3.7.1" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html#replace_na"><i class="fa fa-check"></i><b>3.7.1</b> replace_na</a></li>
<li class="chapter" data-level="3.7.2" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html#fill"><i class="fa fa-check"></i><b>3.7.2</b> fill</a></li>
<li class="chapter" data-level="3.7.3" data-path="tidyr-fill-NA.html"><a href="tidyr-fill-NA.html#drop_na"><i class="fa fa-check"></i><b>3.7.3</b> drop_na</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="character-handling.html"><a href="character-handling.html"><i class="fa fa-check"></i><b>4</b> 字符处理</a>
<ul>
<li class="chapter" data-level="4.1" data-path="character-base-R.html"><a href="character-base-R.html"><i class="fa fa-check"></i><b>4.1</b> base R</a>
<ul>
<li class="chapter" data-level="4.1.1" data-path="character-base-R.html"><a href="character-base-R.html#单双引号"><i class="fa fa-check"></i><b>4.1.1</b> 单双引号</a></li>
<li class="chapter" data-level="4.1.2" data-path="character-base-R.html"><a href="character-base-R.html#转义"><i class="fa fa-check"></i><b>4.1.2</b> 转义</a></li>
<li class="chapter" data-level="4.1.3" data-path="character-base-R.html"><a href="character-base-R.html#常用函数"><i class="fa fa-check"></i><b>4.1.3</b> 常用函数</a></li>
<li class="chapter" data-level="4.1.4" data-path="character-base-R.html"><a href="character-base-R.html#newfeatures"><i class="fa fa-check"></i><b>4.1.4</b> 新特性</a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html"><i class="fa fa-check"></i><b>4.2</b> stringr</a>
<ul>
<li class="chapter" data-level="4.2.1" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html#stringr-install"><i class="fa fa-check"></i><b>4.2.1</b> 安装</a></li>
<li class="chapter" data-level="4.2.2" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html#stringr-usage"><i class="fa fa-check"></i><b>4.2.2</b> 基本使用</a></li>
<li class="chapter" data-level="4.2.3" data-path="character-stringr-packages.html"><a href="character-stringr-packages.html#stringr-functions"><i class="fa fa-check"></i><b>4.2.3</b> 常用函数</a></li>
</ul></li>
<li class="chapter" data-level="4.3" data-path="character-application.html"><a href="character-application.html"><i class="fa fa-check"></i><b>4.3</b> 综合运用</a>
<ul>
<li class="chapter" data-level="4.3.1" data-path="character-application.html"><a href="character-application.html#实现excel函数"><i class="fa fa-check"></i><b>4.3.1</b> 实现excel函数</a></li>
<li class="chapter" data-level="4.3.2" data-path="character-application.html"><a href="character-application.html#使用案例"><i class="fa fa-check"></i><b>4.3.2</b> 使用案例</a></li>
</ul></li>
<li class="chapter" data-level="4.4" data-path="character-the-difference-stringr-and-base.html"><a href="character-the-difference-stringr-and-base.html"><i class="fa fa-check"></i><b>4.4</b> base 和 stringr</a></li>
<li class="chapter" data-level="4.5" data-path="character-reference-material.html"><a href="character-reference-material.html"><i class="fa fa-check"></i><b>4.5</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="datetime.html"><a href="datetime.html"><i class="fa fa-check"></i><b>5</b> 时间处理</a>
<ul>
<li class="chapter" data-level="5.1" data-path="datetime-base-R.html"><a href="datetime-base-R.html"><i class="fa fa-check"></i><b>5.1</b> base R</a>
<ul>
<li class="chapter" data-level="5.1.1" data-path="datetime-base-R.html"><a href="datetime-base-R.html#the-date-class"><i class="fa fa-check"></i><b>5.1.1</b> Date</a></li>
<li class="chapter" data-level="5.1.2" data-path="datetime-base-R.html"><a href="datetime-base-R.html#the-POSIXct-class"><i class="fa fa-check"></i><b>5.1.2</b> POSIXct</a></li>
<li class="chapter" data-level="5.1.3" data-path="datetime-base-R.html"><a href="datetime-base-R.html#the-POSIXlt-class"><i class="fa fa-check"></i><b>5.1.3</b> POSIXlt</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="lubridate.html"><a href="lubridate.html"><i class="fa fa-check"></i><b>5.2</b> lubridate</a>
<ul>
<li class="chapter" data-level="5.2.1" data-path="lubridate.html"><a href="lubridate.html#lubridate-install"><i class="fa fa-check"></i><b>5.2.1</b> 安装包</a></li>
<li class="chapter" data-level="5.2.2" data-path="lubridate.html"><a href="lubridate.html#get-current-datetime"><i class="fa fa-check"></i><b>5.2.2</b> 当前时间日期</a></li>
<li class="chapter" data-level="5.2.3" data-path="lubridate.html"><a href="lubridate.html#make-datetime"><i class="fa fa-check"></i><b>5.2.3</b> 构造日期时间</a></li>
<li class="chapter" data-level="5.2.4" data-path="lubridate.html"><a href="lubridate.html#parse-datetime"><i class="fa fa-check"></i><b>5.2.4</b> 解析日期时间</a></li>
<li class="chapter" data-level="5.2.5" data-path="lubridate.html"><a href="lubridate.html#extracting-datetime-information"><i class="fa fa-check"></i><b>5.2.5</b> 提取日期时间成分</a></li>
<li class="chapter" data-level="5.2.6" data-path="lubridate.html"><a href="lubridate.html#time-zones"><i class="fa fa-check"></i><b>5.2.6</b> 处理时区</a></li>
<li class="chapter" data-level="5.2.7" data-path="lubridate.html"><a href="lubridate.html#interveal"><i class="fa fa-check"></i><b>5.2.7</b> 时间间隔</a></li>
<li class="chapter" data-level="5.2.8" data-path="lubridate.html"><a href="lubridate.html#calculate-datetime"><i class="fa fa-check"></i><b>5.2.8</b> 时间日期计算</a></li>
</ul></li>
<li class="chapter" data-level="5.3" data-path="datetime-application.html"><a href="datetime-application.html"><i class="fa fa-check"></i><b>5.3</b> 综合运用</a>
<ul>
<li class="chapter" data-level="5.3.1" data-path="datetime-application.html"><a href="datetime-application.html#日报同环比计算"><i class="fa fa-check"></i><b>5.3.1</b> 日报同环比计算</a></li>
<li class="chapter" data-level="5.3.2" data-path="datetime-application.html"><a href="datetime-application.html#清洗不同类型日期格式"><i class="fa fa-check"></i><b>5.3.2</b> 清洗不同类型日期格式</a></li>
<li class="chapter" data-level="5.3.3" data-path="datetime-application.html"><a href="datetime-application.html#扫码后中奖时间匹配"><i class="fa fa-check"></i><b>5.3.3</b> 扫码后中奖时间匹配</a></li>
</ul></li>
<li class="chapter" data-level="5.4" data-path="datetime-additional-information.html"><a href="datetime-additional-information.html"><i class="fa fa-check"></i><b>5.4</b> 补充资料</a>
<ul>
<li class="chapter" data-level="5.4.1" data-path="datetime-additional-information.html"><a href="datetime-additional-information.html#excel-and-r"><i class="fa fa-check"></i><b>5.4.1</b> Excel and R</a></li>
<li class="chapter" data-level="5.4.2" data-path="datetime-additional-information.html"><a href="datetime-additional-information.html#参考资料-1"><i class="fa fa-check"></i><b>5.4.2</b> 参考资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="6" data-path="forcats.html"><a href="forcats.html"><i class="fa fa-check"></i><b>6</b> 因子介绍</a>
<ul>
<li class="chapter" data-level="6.1" data-path="forcats-introduction.html"><a href="forcats-introduction.html"><i class="fa fa-check"></i><b>6.1</b> 介绍</a>
<ul>
<li class="chapter" data-level="6.1.1" data-path="forcats-introduction.html"><a href="forcats-introduction.html#forcats:install"><i class="fa fa-check"></i><b>6.1.1</b> 安装</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="forcats-usage.html"><a href="forcats-usage.html"><i class="fa fa-check"></i><b>6.2</b> 用法</a>
<ul>
<li class="chapter" data-level="6.2.1" data-path="forcats-usage.html"><a href="forcats-usage.html#创建因子"><i class="fa fa-check"></i><b>6.2.1</b> 创建因子</a></li>
<li class="chapter" data-level="6.2.2" data-path="forcats-usage.html"><a href="forcats-usage.html#修改因子顺序"><i class="fa fa-check"></i><b>6.2.2</b> 修改因子顺序</a></li>
<li class="chapter" data-level="6.2.3" data-path="forcats-usage.html"><a href="forcats-usage.html#修改因子级别"><i class="fa fa-check"></i><b>6.2.3</b> 修改因子级别</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="7" data-path="Data-Manipulation-datatable.html"><a href="Data-Manipulation-datatable.html"><i class="fa fa-check"></i><b>7</b> data.table</a>
<ul>
<li class="chapter" data-level="7.1" data-path="datatable-description.html"><a href="datatable-description.html"><i class="fa fa-check"></i><b>7.1</b> 基础介绍</a>
<ul>
<li class="chapter" data-level="7.1.1" data-path="datatable-description.html"><a href="datatable-description.html#读取数据"><i class="fa fa-check"></i><b>7.1.1</b> 读取数据</a></li>
<li class="chapter" data-level="7.1.2" data-path="datatable-description.html"><a href="datatable-description.html#基本格式"><i class="fa fa-check"></i><b>7.1.2</b> 基本格式</a></li>
<li class="chapter" data-level="7.1.3" data-path="datatable-description.html"><a href="datatable-description.html#i-j-by-使用"><i class="fa fa-check"></i><b>7.1.3</b> i j by 使用</a></li>
<li class="chapter" data-level="7.1.4" data-path="datatable-description.html"><a href="datatable-description.html#行列筛选总结"><i class="fa fa-check"></i><b>7.1.4</b> 行列筛选总结</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="datatable-usage.html"><a href="datatable-usage.html"><i class="fa fa-check"></i><b>7.2</b> 常规操作</a>
<ul>
<li class="chapter" data-level="7.2.1" data-path="datatable-usage.html"><a href="datatable-usage.html#行筛选"><i class="fa fa-check"></i><b>7.2.1</b> 行筛选</a></li>
<li class="chapter" data-level="7.2.2" data-path="datatable-usage.html"><a href="datatable-usage.html#新增更新列"><i class="fa fa-check"></i><b>7.2.2</b> 新增更新列</a></li>
<li class="chapter" data-level="7.2.3" data-path="datatable-usage.html"><a href="datatable-usage.html#排序"><i class="fa fa-check"></i><b>7.2.3</b> 排序</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="datatable-function.html"><a href="datatable-function.html"><i class="fa fa-check"></i><b>7.3</b> 常用函数</a>
<ul>
<li class="chapter" data-level="7.3.1" data-path="datatable-function.html"><a href="datatable-function.html#特殊符号"><i class="fa fa-check"></i><b>7.3.1</b> 特殊符号</a></li>
<li class="chapter" data-level="7.3.2" data-path="datatable-function.html"><a href="datatable-function.html#排序函数-1"><i class="fa fa-check"></i><b>7.3.2</b> 排序函数</a></li>
<li class="chapter" data-level="7.3.3" data-path="datatable-function.html"><a href="datatable-function.html#非重复计数"><i class="fa fa-check"></i><b>7.3.3</b> 非重复计数</a></li>
<li class="chapter" data-level="7.3.4" data-path="datatable-function.html"><a href="datatable-function.html#判断函数"><i class="fa fa-check"></i><b>7.3.4</b> 判断函数</a></li>
<li class="chapter" data-level="7.3.5" data-path="datatable-function.html"><a href="datatable-function.html#交集-差集-合并"><i class="fa fa-check"></i><b>7.3.5</b> 交集 差集 合并</a></li>
<li class="chapter" data-level="7.3.6" data-path="datatable-function.html"><a href="datatable-function.html#长宽转换"><i class="fa fa-check"></i><b>7.3.6</b> 长宽转换</a></li>
<li class="chapter" data-level="7.3.7" data-path="datatable-function.html"><a href="datatable-function.html#表连接"><i class="fa fa-check"></i><b>7.3.7</b> 表连接</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html"><i class="fa fa-check"></i><b>7.4</b> 高级函数</a>
<ul>
<li class="chapter" data-level="7.4.1" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#groupingsets"><i class="fa fa-check"></i><b>7.4.1</b> groupingsets</a></li>
<li class="chapter" data-level="7.4.2" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#rleid"><i class="fa fa-check"></i><b>7.4.2</b> rleid</a></li>
<li class="chapter" data-level="7.4.3" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#shift"><i class="fa fa-check"></i><b>7.4.3</b> shift</a></li>
<li class="chapter" data-level="7.4.4" data-path="datatable-advanced-functions.html"><a href="datatable-advanced-functions.html#j"><i class="fa fa-check"></i><b>7.4.4</b> J</a></li>
</ul></li>
<li class="chapter" data-level="7.5" data-path="datatable-tips.html"><a href="datatable-tips.html"><i class="fa fa-check"></i><b>7.5</b> 小技巧</a>
<ul>
<li class="chapter" data-level="7.5.1" data-path="datatable-tips.html"><a href="datatable-tips.html#用抑制中间过程输出"><i class="fa fa-check"></i><b>7.5.1</b> 用{}抑制中间过程输出</a></li>
<li class="chapter" data-level="7.5.2" data-path="datatable-tips.html"><a href="datatable-tips.html#使用打印data.table"><i class="fa fa-check"></i><b>7.5.2</b> 使用[]打印data.table</a></li>
</ul></li>
<li class="chapter" data-level="7.6" data-path="datatable-application.html"><a href="datatable-application.html"><i class="fa fa-check"></i><b>7.6</b> 运用</a>
<ul>
<li class="chapter" data-level="7.6.1" data-path="datatable-application.html"><a href="datatable-application.html#自定义函数计算"><i class="fa fa-check"></i><b>7.6.1</b> 自定义函数计算</a></li>
<li class="chapter" data-level="7.6.2" data-path="datatable-application.html"><a href="datatable-application.html#带汇总的聚合运算"><i class="fa fa-check"></i><b>7.6.2</b> 带汇总的聚合运算</a></li>
<li class="chapter" data-level="7.6.3" data-path="datatable-application.html"><a href="datatable-application.html#行列转变"><i class="fa fa-check"></i><b>7.6.3</b> 行列转变</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="8" data-path="database.html"><a href="database.html"><i class="fa fa-check"></i><b>8</b> database</a>
<ul>
<li class="chapter" data-level="8.1" data-path="database-install.html"><a href="database-install.html"><i class="fa fa-check"></i><b>8.1</b> 安装数据库</a></li>
<li class="chapter" data-level="8.2" data-path="database-odbc-package.html"><a href="database-odbc-package.html"><i class="fa fa-check"></i><b>8.2</b> odbc包</a>
<ul>
<li class="chapter" data-level="8.2.1" data-path="database-odbc-package.html"><a href="database-odbc-package.html#安装包"><i class="fa fa-check"></i><b>8.2.1</b> 安装包</a></li>
<li class="chapter" data-level="8.2.2" data-path="database-odbc-package.html"><a href="database-odbc-package.html#驱动安装"><i class="fa fa-check"></i><b>8.2.2</b> 驱动安装</a></li>
<li class="chapter" data-level="8.2.3" data-path="database-odbc-package.html"><a href="database-odbc-package.html#连接数据库"><i class="fa fa-check"></i><b>8.2.3</b> 连接数据库</a></li>
<li class="chapter" data-level="8.2.4" data-path="database-odbc-package.html"><a href="database-odbc-package.html#读写数据库"><i class="fa fa-check"></i><b>8.2.4</b> 读写数据库</a></li>
</ul></li>
<li class="chapter" data-level="8.3" data-path="database-DBI-package.html"><a href="database-DBI-package.html"><i class="fa fa-check"></i><b>8.3</b> DBI包</a>
<ul>
<li class="chapter" data-level="8.3.1" data-path="database-DBI-package.html"><a href="database-DBI-package.html#安装-1"><i class="fa fa-check"></i><b>8.3.1</b> 安装</a></li>
<li class="chapter" data-level="8.3.2" data-path="database-DBI-package.html"><a href="database-DBI-package.html#连接数据库-1"><i class="fa fa-check"></i><b>8.3.2</b> 连接数据库</a></li>
<li class="chapter" data-level="8.3.3" data-path="database-DBI-package.html"><a href="database-DBI-package.html#读写数据库-1"><i class="fa fa-check"></i><b>8.3.3</b> 读写数据库</a></li>
<li class="chapter" data-level="8.3.4" data-path="database-DBI-package.html"><a href="database-DBI-package.html#其它函数介绍"><i class="fa fa-check"></i><b>8.3.4</b> 其它函数介绍</a></li>
</ul></li>
<li class="chapter" data-level="8.4" data-path="database-else-package.html"><a href="database-else-package.html"><i class="fa fa-check"></i><b>8.4</b> 其他R包</a>
<ul>
<li class="chapter" data-level="8.4.1" data-path="database-else-package.html"><a href="database-else-package.html#rodbc包"><i class="fa fa-check"></i><b>8.4.1</b> RODBC包</a></li>
<li class="chapter" data-level="8.4.2" data-path="database-else-package.html"><a href="database-else-package.html#roracle包"><i class="fa fa-check"></i><b>8.4.2</b> ROracle包</a></li>
<li class="chapter" data-level="8.4.3" data-path="database-else-package.html"><a href="database-else-package.html#rmysql包"><i class="fa fa-check"></i><b>8.4.3</b> RMySQL包</a></li>
<li class="chapter" data-level="8.4.4" data-path="database-else-package.html"><a href="database-else-package.html#rsqlite"><i class="fa fa-check"></i><b>8.4.4</b> RSQLite</a></li>
</ul></li>
<li class="chapter" data-level="8.5" data-path="database-big-data.html"><a href="database-big-data.html"><i class="fa fa-check"></i><b>8.5</b> 大数据相关R包</a>
<ul>
<li class="chapter" data-level="8.5.1" data-path="database-big-data.html"><a href="database-big-data.html#sparklyr"><i class="fa fa-check"></i><b>8.5.1</b> sparklyr</a></li>
<li class="chapter" data-level="8.5.2" data-path="database-big-data.html"><a href="database-big-data.html#RClickhouse"><i class="fa fa-check"></i><b>8.5.2</b> RClickhouse</a></li>
</ul></li>
<li class="chapter" data-level="8.6" data-path="database-FAQ.html"><a href="database-FAQ.html"><i class="fa fa-check"></i><b>8.6</b> 常见问题</a>
<ul>
<li class="chapter" data-level="8.6.1" data-path="database-FAQ.html"><a href="database-FAQ.html#乱码问题"><i class="fa fa-check"></i><b>8.6.1</b> 乱码问题</a></li>
<li class="chapter" data-level="8.6.2" data-path="database-FAQ.html"><a href="database-FAQ.html#无法连接问题"><i class="fa fa-check"></i><b>8.6.2</b> 无法连接问题</a></li>
<li class="chapter" data-level="8.6.3" data-path="database-FAQ.html"><a href="database-FAQ.html#远程连接"><i class="fa fa-check"></i><b>8.6.3</b> 远程连接</a></li>
<li class="chapter" data-level="8.6.4" data-path="database-FAQ.html"><a href="database-FAQ.html#跳板机连接"><i class="fa fa-check"></i><b>8.6.4</b> 跳板机连接</a></li>
</ul></li>
<li class="chapter" data-level="8.7" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html"><i class="fa fa-check"></i><b>8.7</b> dbplyr</a>
<ul>
<li class="chapter" data-level="8.7.1" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html#安装-2"><i class="fa fa-check"></i><b>8.7.1</b> 安装</a></li>
<li class="chapter" data-level="8.7.2" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html#基础用法-1"><i class="fa fa-check"></i><b>8.7.2</b> 基础用法</a></li>
<li class="chapter" data-level="8.7.3" data-path="database-dbplyr-package.html"><a href="database-dbplyr-package.html#无法正确转化"><i class="fa fa-check"></i><b>8.7.3</b> 无法正确转化</a></li>
</ul></li>
<li class="chapter" data-level="8.8" data-path="database-reference-material.html"><a href="database-reference-material.html"><i class="fa fa-check"></i><b>8.8</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="R-loop-structure.html"><a href="R-loop-structure.html"><i class="fa fa-check"></i><b>9</b> 循环结构</a>
<ul>
<li class="chapter" data-level="9.1" data-path="R-loop-description.html"><a href="R-loop-description.html"><i class="fa fa-check"></i><b>9.1</b> R循环介绍</a>
<ul>
<li class="chapter" data-level="9.1.1" data-path="R-loop-description.html"><a href="R-loop-description.html#R:loop-example"><i class="fa fa-check"></i><b>9.1.1</b> 简单示例</a></li>
<li class="chapter" data-level="9.1.2" data-path="R-loop-description.html"><a href="R-loop-description.html#three:loop-structure"><i class="fa fa-check"></i><b>9.1.2</b> 循环结构</a></li>
<li class="chapter" data-level="9.1.3" data-path="R-loop-description.html"><a href="R-loop-description.html#loop:control"><i class="fa fa-check"></i><b>9.1.3</b> 循环控制</a></li>
<li class="chapter" data-level="9.1.4" data-path="R-loop-description.html"><a href="R-loop-description.html#嵌套循环"><i class="fa fa-check"></i><b>9.1.4</b> 嵌套循环</a></li>
</ul></li>
<li class="chapter" data-level="9.2" data-path="loop-changes.html"><a href="loop-changes.html"><i class="fa fa-check"></i><b>9.2</b> 循环变化</a>
<ul>
<li class="chapter" data-level="9.2.1" data-path="loop-changes.html"><a href="loop-changes.html#修改已有对象"><i class="fa fa-check"></i><b>9.2.1</b> 修改已有对象</a></li>
<li class="chapter" data-level="9.2.2" data-path="loop-changes.html"><a href="loop-changes.html#循环模式"><i class="fa fa-check"></i><b>9.2.2</b> 循环模式</a></li>
<li class="chapter" data-level="9.2.3" data-path="loop-changes.html"><a href="loop-changes.html#未知长度输出"><i class="fa fa-check"></i><b>9.2.3</b> 未知长度输出</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="apply-function.html"><a href="apply-function.html"><i class="fa fa-check"></i><b>9.3</b> apply系列函数</a>
<ul>
<li class="chapter" data-level="9.3.1" data-path="apply-function.html"><a href="apply-function.html#lapply"><i class="fa fa-check"></i><b>9.3.1</b> lapply 循环迭代</a></li>
<li class="chapter" data-level="9.3.2" data-path="apply-function.html"><a href="apply-function.html#sapply"><i class="fa fa-check"></i><b>9.3.2</b> sapply</a></li>
<li class="chapter" data-level="9.3.3" data-path="apply-function.html"><a href="apply-function.html#vapply"><i class="fa fa-check"></i><b>9.3.3</b> vapply 迭代安全函数</a></li>
<li class="chapter" data-level="9.3.4" data-path="apply-function.html"><a href="apply-function.html#apply"><i class="fa fa-check"></i><b>9.3.4</b> apply 多维数据</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="10" data-path="purrr-Iteration.html"><a href="purrr-Iteration.html"><i class="fa fa-check"></i><b>10</b> 循环迭代purrr介绍</a>
<ul>
<li class="chapter" data-level="10.1" data-path="安装-3.html"><a href="安装-3.html"><i class="fa fa-check"></i><b>10.1</b> 安装</a></li>
<li class="chapter" data-level="10.2" data-path="purrr-basic-usage.html"><a href="purrr-basic-usage.html"><i class="fa fa-check"></i><b>10.2</b> 常规用法</a>
<ul>
<li class="chapter" data-level="10.2.1" data-path="purrr-basic-usage.html"><a href="purrr-basic-usage.html#map-系列"><i class="fa fa-check"></i><b>10.2.1</b> map 系列</a></li>
<li class="chapter" data-level="10.2.2" data-path="purrr-basic-usage.html"><a href="purrr-basic-usage.html#pmap-系列"><i class="fa fa-check"></i><b>10.2.2</b> pmap 系列</a></li>
</ul></li>
<li class="chapter" data-level="10.3" data-path="purrr-map.html"><a href="purrr-map.html"><i class="fa fa-check"></i><b>10.3</b> map系列函数</a>
<ul>
<li class="chapter" data-level="10.3.1" data-path="purrr-map.html"><a href="purrr-map.html#用法-3"><i class="fa fa-check"></i><b>10.3.1</b> 用法</a></li>
<li class="chapter" data-level="10.3.2" data-path="purrr-map.html"><a href="purrr-map.html#案例-5"><i class="fa fa-check"></i><b>10.3.2</b> 案例</a></li>
</ul></li>
<li class="chapter" data-level="10.4" data-path="purrr-map2.html"><a href="purrr-map2.html"><i class="fa fa-check"></i><b>10.4</b> map2 系列函数</a></li>
<li class="chapter" data-level="10.5" data-path="purrr-multi-parameter.html"><a href="purrr-multi-parameter.html"><i class="fa fa-check"></i><b>10.5</b> pmap系列函数</a></li>
<li class="chapter" data-level="10.6" data-path="purrr-reduce.html"><a href="purrr-reduce.html"><i class="fa fa-check"></i><b>10.6</b> 归约累计函数</a>
<ul>
<li class="chapter" data-level="10.6.1" data-path="purrr-reduce.html"><a href="purrr-reduce.html#reduce-函数"><i class="fa fa-check"></i><b>10.6.1</b> reduce 函数</a></li>
<li class="chapter" data-level="10.6.2" data-path="purrr-reduce.html"><a href="purrr-reduce.html#accumulate-函数"><i class="fa fa-check"></i><b>10.6.2</b> accumulate 函数</a></li>
</ul></li>
<li class="chapter" data-level="10.7" data-path="purrr-safely-functions.html"><a href="purrr-safely-functions.html"><i class="fa fa-check"></i><b>10.7</b> 安全函数</a></li>
<li class="chapter" data-level="10.8" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html"><i class="fa fa-check"></i><b>10.8</b> 其他函数介绍</a>
<ul>
<li class="chapter" data-level="10.8.1" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#flatten-系列"><i class="fa fa-check"></i><b>10.8.1</b> flatten 系列</a></li>
<li class="chapter" data-level="10.8.2" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#imap-函数"><i class="fa fa-check"></i><b>10.8.2</b> imap 函数</a></li>
<li class="chapter" data-level="10.8.3" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#walk-系列"><i class="fa fa-check"></i><b>10.8.3</b> walk 系列</a></li>
<li class="chapter" data-level="10.8.4" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#逻辑判断函数"><i class="fa fa-check"></i><b>10.8.4</b> 逻辑判断函数</a></li>
<li class="chapter" data-level="10.8.5" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#提取函数"><i class="fa fa-check"></i><b>10.8.5</b> 提取函数</a></li>
<li class="chapter" data-level="10.8.6" data-path="purrr-else-functions.html"><a href="purrr-else-functions.html#筛选函数"><i class="fa fa-check"></i><b>10.8.6</b> 筛选函数</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="11" data-path="self-define-function.html"><a href="self-define-function.html"><i class="fa fa-check"></i><b>11</b> 自定义函数</a>
<ul>
<li class="chapter" data-level="11.1" data-path="define-function-example.html"><a href="define-function-example.html"><i class="fa fa-check"></i><b>11.1</b> 简单示例</a></li>
<li class="chapter" data-level="11.2" data-path="define-function-conditional-execution.html"><a href="define-function-conditional-execution.html"><i class="fa fa-check"></i><b>11.2</b> 条件执行</a>
<ul>
<li class="chapter" data-level="11.2.1" data-path="define-function-conditional-execution.html"><a href="define-function-conditional-execution.html#多条件执行"><i class="fa fa-check"></i><b>11.2.1</b> 多条件执行</a></li>
</ul></li>
<li class="chapter" data-level="11.3" data-path="define-function-parameter.html"><a href="define-function-parameter.html"><i class="fa fa-check"></i><b>11.3</b> 函数参数</a>
<ul>
<li class="chapter" data-level="11.3.1" data-path="define-function-parameter.html"><a href="define-function-parameter.html#参数名称"><i class="fa fa-check"></i><b>11.3.1</b> 参数名称</a></li>
<li class="chapter" data-level="11.3.2" data-path="define-function-parameter.html"><a href="define-function-parameter.html#检查参数值"><i class="fa fa-check"></i><b>11.3.2</b> 检查参数值</a></li>
<li class="chapter" data-level="11.3.3" data-path="define-function-parameter.html"><a href="define-function-parameter.html#参数-5"><i class="fa fa-check"></i><b>11.3.3</b> …参数</a></li>
</ul></li>
<li class="chapter" data-level="11.4" data-path="function-return.html"><a href="function-return.html"><i class="fa fa-check"></i><b>11.4</b> 返回值</a>
<ul>
<li class="chapter" data-level="11.4.1" data-path="function-return.html"><a href="function-return.html#显式返回"><i class="fa fa-check"></i><b>11.4.1</b> 显式返回</a></li>
<li class="chapter" data-level="11.4.2" data-path="function-return.html"><a href="function-return.html#编写管道函数"><i class="fa fa-check"></i><b>11.4.2</b> 编写管道函数</a></li>
</ul></li>
<li class="chapter" data-level="11.5" data-path="function-environment.html"><a href="function-environment.html"><i class="fa fa-check"></i><b>11.5</b> 环境</a></li>
<li class="chapter" data-level="11.6" data-path="function-expand.html"><a href="function-expand.html"><i class="fa fa-check"></i><b>11.6</b> 拓展部分</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="file-system-operation.html"><a href="file-system-operation.html"><i class="fa fa-check"></i><b>12</b> R语言文件系统操作</a>
<ul>
<li class="chapter" data-level="12.1" data-path="file-base-r.html"><a href="file-base-r.html"><i class="fa fa-check"></i><b>12.1</b> base R</a>
<ul>
<li class="chapter" data-level="12.1.1" data-path="file-base-r.html"><a href="file-base-r.html#base:r-usage"><i class="fa fa-check"></i><b>12.1.1</b> 用法</a></li>
</ul></li>
<li class="chapter" data-level="12.2" data-path="fs-package.html"><a href="fs-package.html"><i class="fa fa-check"></i><b>12.2</b> fs package</a>
<ul>
<li class="chapter" data-level="12.2.1" data-path="fs-package.html"><a href="fs-package.html#fs-install"><i class="fa fa-check"></i><b>12.2.1</b> 安装</a></li>
<li class="chapter" data-level="12.2.2" data-path="fs-package.html"><a href="fs-package.html#fs-advanced"><i class="fa fa-check"></i><b>12.2.2</b> 优势</a></li>
<li class="chapter" data-level="12.2.3" data-path="fs-package.html"><a href="fs-package.html#fs-usage"><i class="fa fa-check"></i><b>12.2.3</b> 基础用法</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="fs-functions.html"><a href="fs-functions.html"><i class="fa fa-check"></i><b>12.3</b> fs函数介绍</a>
<ul>
<li class="chapter" data-level="12.3.1" data-path="fs-functions.html"><a href="fs-functions.html#fs-file-manipulation"><i class="fa fa-check"></i><b>12.3.1</b> 文件操作</a></li>
<li class="chapter" data-level="12.3.2" data-path="fs-functions.html"><a href="fs-functions.html#fs-dir-manipulation"><i class="fa fa-check"></i><b>12.3.2</b> 目录操作</a></li>
<li class="chapter" data-level="12.3.3" data-path="fs-functions.html"><a href="fs-functions.html#fs-link-manipulation"><i class="fa fa-check"></i><b>12.3.3</b> 链接操作</a></li>
<li class="chapter" data-level="12.3.4" data-path="fs-functions.html"><a href="fs-functions.html#fs-path-manipulation"><i class="fa fa-check"></i><b>12.3.4</b> 路径操作</a></li>
<li class="chapter" data-level="12.3.5" data-path="fs-functions.html"><a href="fs-functions.html#fs-help-manipulation"><i class="fa fa-check"></i><b>12.3.5</b> 辅助函数</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="file-base-fs-difference.html"><a href="file-base-fs-difference.html"><i class="fa fa-check"></i><b>12.4</b> base-fs-shell比较</a></li>
<li class="chapter" data-level="12.5" data-path="file-manipulation-reference-material.html"><a href="file-manipulation-reference-material.html"><i class="fa fa-check"></i><b>12.5</b> 参考资料</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://gitee.com/zhongyufei/Rbook" target="blank">gitee地址</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">商业数据分析师-R 语言数据处理</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="pivoting" class="section level2" number="3.2">
<h2><span class="header-section-number">3.2</span> 长宽转换</h2>
<p>我们在使用 excel 的数据透视表时，即将“长数据” 转化成“宽数据,”这就是数据的长宽转化。有关<code>pivot</code>详情，请查看<code>vignette("pivot")</code>。
接下来我们用<code>tidyr</code>包自带的插图案例查看相关函数用法。</p>
<div id="宽转长" class="section level3" number="3.2.1">
<h3><span class="header-section-number">3.2.1</span> 宽转长</h3>
<p>有时候方便我们肉眼观察，可能一个数据集会有很多列,
方便观察，但是不方便统计分析，这是我们需要把数据做处理，从“宽数据变成长数据”即宽转长。如下案例：</p>
<table>
<thead>
<tr class="header">
<th>分公司</th>
<th>商品A</th>
<th>商品B</th>
<th>商品C</th>
<th>商品D</th>
<th>商品E</th>
<th>商品F</th>
<th>商品G</th>
<th>商品H</th>
<th>商品I</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>北京</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr class="even">
<td>深圳</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr class="odd">
<td>上海</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr class="even">
<td>广州</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr class="odd">
<td>成都</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr class="even">
<td>重庆</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr class="odd">
<td>长沙</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</tbody>
</table>
<p>将上面的数据集从“宽数据集”转化成“长数据集”。</p>
<div class="sourceCode" id="cb198"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb198-1"><a href="pivoting.html#cb198-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse,<span class="at">warn.conflicts =</span> <span class="cn">FALSE</span>)</span>
<span id="cb198-2"><a href="pivoting.html#cb198-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyr)</span></code></pre></div>
<p>准备数据集 dt</p>
<div class="sourceCode" id="cb199"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb199-1"><a href="pivoting.html#cb199-1" aria-hidden="true" tabindex="-1"></a>dt <span class="ot">&lt;-</span> tibble<span class="sc">::</span><span class="fu">tribble</span>(</span>
<span id="cb199-2"><a href="pivoting.html#cb199-2" aria-hidden="true" tabindex="-1"></a>  <span class="sc">~</span>分公司, <span class="sc">~</span>商品A, <span class="sc">~</span>商品B, <span class="sc">~</span>商品C, <span class="sc">~</span>商品D, <span class="sc">~</span>商品E, <span class="sc">~</span>商品F, <span class="sc">~</span>商品G, <span class="sc">~</span>商品H, <span class="sc">~</span>商品I,</span>
<span id="cb199-3"><a href="pivoting.html#cb199-3" aria-hidden="true" tabindex="-1"></a>  <span class="st">&quot;北京&quot;</span>,   1L,   2L,   3L,   4L,   5L,   6L,   7L,   8L,   9L,</span>
<span id="cb199-4"><a href="pivoting.html#cb199-4" aria-hidden="true" tabindex="-1"></a>  <span class="st">&quot;深圳&quot;</span>,   1L,   2L,   3L,   4L,   5L,   6L,   7L,   8L,   9L,</span>
<span id="cb199-5"><a href="pivoting.html#cb199-5" aria-hidden="true" tabindex="-1"></a>  <span class="st">&quot;上海&quot;</span>,   1L,   2L,   3L,   4L,   5L,   6L,   7L,   8L,   9L,</span>
<span id="cb199-6"><a href="pivoting.html#cb199-6" aria-hidden="true" tabindex="-1"></a>  <span class="st">&quot;广州&quot;</span>,   1L,   2L,   3L,   4L,   5L,   6L,   7L,   8L,   9L,</span>
<span id="cb199-7"><a href="pivoting.html#cb199-7" aria-hidden="true" tabindex="-1"></a>  <span class="st">&quot;成都&quot;</span>,   1L,   2L,   3L,   4L,   5L,   6L,   7L,   8L,   9L,</span>
<span id="cb199-8"><a href="pivoting.html#cb199-8" aria-hidden="true" tabindex="-1"></a>  <span class="st">&quot;重庆&quot;</span>,   1L,   2L,   3L,   4L,   5L,   6L,   7L,   8L,   9L,</span>
<span id="cb199-9"><a href="pivoting.html#cb199-9" aria-hidden="true" tabindex="-1"></a>  <span class="st">&quot;长沙&quot;</span>,   1L,   2L,   3L,   4L,   5L,   6L,   7L,   8L,   9L</span>
<span id="cb199-10"><a href="pivoting.html#cb199-10" aria-hidden="true" tabindex="-1"></a>  )</span></code></pre></div>
<p>经过转化，数据由 10 列变成 3 列；</p>
<div class="sourceCode" id="cb200"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb200-1"><a href="pivoting.html#cb200-1" aria-hidden="true" tabindex="-1"></a>dt <span class="sc">%&gt;%</span> </span>
<span id="cb200-2"><a href="pivoting.html#cb200-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">pivot_longer</span>(<span class="at">cols =</span> 商品A<span class="sc">:</span>商品I,<span class="at">names_to =</span> <span class="st">&#39;商品名称&#39;</span>,<span class="at">values_to =</span> <span class="st">&#39;销量&#39;</span>) <span class="sc">%&gt;%</span> </span>
<span id="cb200-3"><a href="pivoting.html#cb200-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">head</span>()</span>
<span id="cb200-4"><a href="pivoting.html#cb200-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 6 x 3</span></span>
<span id="cb200-5"><a href="pivoting.html#cb200-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   分公司 商品名称  销量</span></span>
<span id="cb200-6"><a href="pivoting.html#cb200-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;  &lt;chr&gt;    &lt;int&gt;</span></span>
<span id="cb200-7"><a href="pivoting.html#cb200-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 北京   商品A        1</span></span>
<span id="cb200-8"><a href="pivoting.html#cb200-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 北京   商品B        2</span></span>
<span id="cb200-9"><a href="pivoting.html#cb200-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 北京   商品C        3</span></span>
<span id="cb200-10"><a href="pivoting.html#cb200-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 北京   商品D        4</span></span>
<span id="cb200-11"><a href="pivoting.html#cb200-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 北京   商品E        5</span></span>
<span id="cb200-12"><a href="pivoting.html#cb200-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 北京   商品F        6</span></span></code></pre></div>
</div>
<div id="用法-1" class="section level3" number="3.2.2">
<h3><span class="header-section-number">3.2.2</span> 用法</h3>
<div class="sourceCode" id="cb201"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb201-1"><a href="pivoting.html#cb201-1" aria-hidden="true" tabindex="-1"></a><span class="fu">pivot_longer</span>(</span>
<span id="cb201-2"><a href="pivoting.html#cb201-2" aria-hidden="true" tabindex="-1"></a>  data,</span>
<span id="cb201-3"><a href="pivoting.html#cb201-3" aria-hidden="true" tabindex="-1"></a>  cols,</span>
<span id="cb201-4"><a href="pivoting.html#cb201-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_to =</span> <span class="st">&quot;name&quot;</span>,</span>
<span id="cb201-5"><a href="pivoting.html#cb201-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_prefix =</span> <span class="cn">NULL</span>,</span>
<span id="cb201-6"><a href="pivoting.html#cb201-6" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_sep =</span> <span class="cn">NULL</span>,</span>
<span id="cb201-7"><a href="pivoting.html#cb201-7" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_pattern =</span> <span class="cn">NULL</span>,</span>
<span id="cb201-8"><a href="pivoting.html#cb201-8" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_ptypes =</span> <span class="fu">list</span>(),</span>
<span id="cb201-9"><a href="pivoting.html#cb201-9" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_transform =</span> <span class="fu">list</span>(),</span>
<span id="cb201-10"><a href="pivoting.html#cb201-10" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_repair =</span> <span class="st">&quot;check_unique&quot;</span>,</span>
<span id="cb201-11"><a href="pivoting.html#cb201-11" aria-hidden="true" tabindex="-1"></a>  <span class="at">values_to =</span> <span class="st">&quot;value&quot;</span>,</span>
<span id="cb201-12"><a href="pivoting.html#cb201-12" aria-hidden="true" tabindex="-1"></a>  <span class="at">values_drop_na =</span> <span class="cn">FALSE</span>,</span>
<span id="cb201-13"><a href="pivoting.html#cb201-13" aria-hidden="true" tabindex="-1"></a>  <span class="at">values_ptypes =</span> <span class="fu">list</span>(),</span>
<span id="cb201-14"><a href="pivoting.html#cb201-14" aria-hidden="true" tabindex="-1"></a>  <span class="at">values_transform =</span> <span class="fu">list</span>(),</span>
<span id="cb201-15"><a href="pivoting.html#cb201-15" aria-hidden="true" tabindex="-1"></a>  ...</span>
<span id="cb201-16"><a href="pivoting.html#cb201-16" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<ul>
<li><code>data</code>: 数据集</li>
<li><code>cols</code>: 需要需要重塑，在上例中除了分公司的其他全部列</li>
<li><code>names_to</code>: 新增的列名，即重塑列的新列名，如上列的商品名称</li>
<li><code>values_to</code>: 新增存储数据新列名，如上列的销量</li>
<li><code>names_prefix</code>: 一个正则表达式用来删除列名中匹配到的字符</li>
<li><code>values_drop_na</code>: 是否删除空值行，默认为FALSE</li>
</ul>
<p>其余参数的使用比较复杂，对于初学而言可以不掌握，当有更高阶需求的时候自行查函数手册了解用法即可。接下来了解几种常见的使用场景。</p>
<div id="列名带数字" class="section level4" number="3.2.2.1">
<h4><span class="header-section-number">3.2.2.1</span> 列名带数字</h4>
<div class="sourceCode" id="cb202"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb202-1"><a href="pivoting.html#cb202-1" aria-hidden="true" tabindex="-1"></a>billboard <span class="sc">%&gt;%</span> </span>
<span id="cb202-2"><a href="pivoting.html#cb202-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">pivot_longer</span>(</span>
<span id="cb202-3"><a href="pivoting.html#cb202-3" aria-hidden="true" tabindex="-1"></a>    <span class="at">cols =</span> <span class="fu">starts_with</span>(<span class="st">&quot;wk&quot;</span>), </span>
<span id="cb202-4"><a href="pivoting.html#cb202-4" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_to =</span> <span class="st">&quot;week&quot;</span>, </span>
<span id="cb202-5"><a href="pivoting.html#cb202-5" aria-hidden="true" tabindex="-1"></a>    <span class="at">values_to =</span> <span class="st">&quot;rank&quot;</span>,</span>
<span id="cb202-6"><a href="pivoting.html#cb202-6" aria-hidden="true" tabindex="-1"></a>    <span class="at">values_drop_na =</span> <span class="cn">TRUE</span></span>
<span id="cb202-7"><a href="pivoting.html#cb202-7" aria-hidden="true" tabindex="-1"></a>  )</span>
<span id="cb202-8"><a href="pivoting.html#cb202-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 5,307 x 5</span></span>
<span id="cb202-9"><a href="pivoting.html#cb202-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   artist track                   date.entered week   rank</span></span>
<span id="cb202-10"><a href="pivoting.html#cb202-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;  &lt;chr&gt;                   &lt;date&gt;       &lt;chr&gt; &lt;dbl&gt;</span></span>
<span id="cb202-11"><a href="pivoting.html#cb202-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 2 Pac  Baby Don&#39;t Cry (Keep... 2000-02-26   wk1      87</span></span>
<span id="cb202-12"><a href="pivoting.html#cb202-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 2 Pac  Baby Don&#39;t Cry (Keep... 2000-02-26   wk2      82</span></span>
<span id="cb202-13"><a href="pivoting.html#cb202-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 2 Pac  Baby Don&#39;t Cry (Keep... 2000-02-26   wk3      72</span></span>
<span id="cb202-14"><a href="pivoting.html#cb202-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 2 Pac  Baby Don&#39;t Cry (Keep... 2000-02-26   wk4      77</span></span>
<span id="cb202-15"><a href="pivoting.html#cb202-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 2 Pac  Baby Don&#39;t Cry (Keep... 2000-02-26   wk5      87</span></span>
<span id="cb202-16"><a href="pivoting.html#cb202-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 2 Pac  Baby Don&#39;t Cry (Keep... 2000-02-26   wk6      94</span></span>
<span id="cb202-17"><a href="pivoting.html#cb202-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 5,301 more rows</span></span></code></pre></div>
<p><code>names_prefix</code> 调整内容前缀，配合<code>names_transform</code>参数使用</p>
<div class="sourceCode" id="cb203"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb203-1"><a href="pivoting.html#cb203-1" aria-hidden="true" tabindex="-1"></a>billboard <span class="sc">%&gt;%</span> </span>
<span id="cb203-2"><a href="pivoting.html#cb203-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">pivot_longer</span>(</span>
<span id="cb203-3"><a href="pivoting.html#cb203-3" aria-hidden="true" tabindex="-1"></a>    <span class="at">cols =</span> <span class="fu">starts_with</span>(<span class="st">&quot;wk&quot;</span>), </span>
<span id="cb203-4"><a href="pivoting.html#cb203-4" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_to =</span> <span class="st">&quot;week&quot;</span>, </span>
<span id="cb203-5"><a href="pivoting.html#cb203-5" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_prefix =</span> <span class="st">&quot;wk&quot;</span>,</span>
<span id="cb203-6"><a href="pivoting.html#cb203-6" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_transform =</span> <span class="fu">list</span>(<span class="at">week =</span> as.integer),</span>
<span id="cb203-7"><a href="pivoting.html#cb203-7" aria-hidden="true" tabindex="-1"></a>    <span class="at">values_to =</span> <span class="st">&quot;rank&quot;</span>,</span>
<span id="cb203-8"><a href="pivoting.html#cb203-8" aria-hidden="true" tabindex="-1"></a>    <span class="at">values_drop_na =</span> <span class="cn">TRUE</span>,</span>
<span id="cb203-9"><a href="pivoting.html#cb203-9" aria-hidden="true" tabindex="-1"></a>  )</span>
<span id="cb203-10"><a href="pivoting.html#cb203-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 5,307 x 5</span></span>
<span id="cb203-11"><a href="pivoting.html#cb203-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   artist track                   date.entered  week  rank</span></span>
<span id="cb203-12"><a href="pivoting.html#cb203-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;  &lt;chr&gt;                   &lt;date&gt;       &lt;int&gt; &lt;dbl&gt;</span></span>
<span id="cb203-13"><a href="pivoting.html#cb203-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 2 Pac  Baby Don&#39;t Cry (Keep... 2000-02-26       1    87</span></span>
<span id="cb203-14"><a href="pivoting.html#cb203-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 2 Pac  Baby Don&#39;t Cry (Keep... 2000-02-26       2    82</span></span>
<span id="cb203-15"><a href="pivoting.html#cb203-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 2 Pac  Baby Don&#39;t Cry (Keep... 2000-02-26       3    72</span></span>
<span id="cb203-16"><a href="pivoting.html#cb203-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 2 Pac  Baby Don&#39;t Cry (Keep... 2000-02-26       4    77</span></span>
<span id="cb203-17"><a href="pivoting.html#cb203-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 2 Pac  Baby Don&#39;t Cry (Keep... 2000-02-26       5    87</span></span>
<span id="cb203-18"><a href="pivoting.html#cb203-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 2 Pac  Baby Don&#39;t Cry (Keep... 2000-02-26       6    94</span></span>
<span id="cb203-19"><a href="pivoting.html#cb203-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 5,301 more rows</span></span></code></pre></div>
<p>经过以上转换<code>week</code>列属性变成了整数，当然达到以上效果有其他的途径，如下：</p>
<div class="sourceCode" id="cb204"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb204-1"><a href="pivoting.html#cb204-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse,<span class="at">warn.conflicts =</span> <span class="cn">TRUE</span>)</span>
<span id="cb204-2"><a href="pivoting.html#cb204-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb204-3"><a href="pivoting.html#cb204-3" aria-hidden="true" tabindex="-1"></a><span class="co"># method 1</span></span>
<span id="cb204-4"><a href="pivoting.html#cb204-4" aria-hidden="true" tabindex="-1"></a>billboard <span class="sc">%&gt;%</span> </span>
<span id="cb204-5"><a href="pivoting.html#cb204-5" aria-hidden="true" tabindex="-1"></a>  <span class="fu">pivot_longer</span>(</span>
<span id="cb204-6"><a href="pivoting.html#cb204-6" aria-hidden="true" tabindex="-1"></a>    <span class="at">cols =</span> <span class="fu">starts_with</span>(<span class="st">&quot;wk&quot;</span>), </span>
<span id="cb204-7"><a href="pivoting.html#cb204-7" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_to =</span> <span class="st">&quot;week&quot;</span>, </span>
<span id="cb204-8"><a href="pivoting.html#cb204-8" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_transform =</span> <span class="fu">list</span>(<span class="at">week =</span> readr<span class="sc">::</span>parse_number),</span>
<span id="cb204-9"><a href="pivoting.html#cb204-9" aria-hidden="true" tabindex="-1"></a>    <span class="at">values_to =</span> <span class="st">&quot;rank&quot;</span>,</span>
<span id="cb204-10"><a href="pivoting.html#cb204-10" aria-hidden="true" tabindex="-1"></a>    <span class="at">values_drop_na =</span> <span class="cn">TRUE</span>,</span>
<span id="cb204-11"><a href="pivoting.html#cb204-11" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb204-12"><a href="pivoting.html#cb204-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb204-13"><a href="pivoting.html#cb204-13" aria-hidden="true" tabindex="-1"></a><span class="co"># method 2</span></span>
<span id="cb204-14"><a href="pivoting.html#cb204-14" aria-hidden="true" tabindex="-1"></a>billboard <span class="sc">%&gt;%</span></span>
<span id="cb204-15"><a href="pivoting.html#cb204-15" aria-hidden="true" tabindex="-1"></a>  <span class="fu">pivot_longer</span>(</span>
<span id="cb204-16"><a href="pivoting.html#cb204-16" aria-hidden="true" tabindex="-1"></a>    <span class="at">cols =</span> <span class="fu">starts_with</span>(<span class="st">&quot;wk&quot;</span>),</span>
<span id="cb204-17"><a href="pivoting.html#cb204-17" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_to =</span> <span class="st">&quot;week&quot;</span>,</span>
<span id="cb204-18"><a href="pivoting.html#cb204-18" aria-hidden="true" tabindex="-1"></a>    <span class="at">values_to =</span> <span class="st">&quot;rank&quot;</span>,</span>
<span id="cb204-19"><a href="pivoting.html#cb204-19" aria-hidden="true" tabindex="-1"></a>    <span class="at">values_drop_na =</span> <span class="cn">TRUE</span>,</span>
<span id="cb204-20"><a href="pivoting.html#cb204-20" aria-hidden="true" tabindex="-1"></a>  ) <span class="sc">%&gt;%</span></span>
<span id="cb204-21"><a href="pivoting.html#cb204-21" aria-hidden="true" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">week =</span> <span class="fu">str_remove</span>(week, <span class="st">&quot;wk&quot;</span>) <span class="sc">%&gt;%</span> <span class="fu">as.integer</span>())</span></code></pre></div>
</div>
<div id="多变量列名" class="section level4" number="3.2.2.2">
<h4><span class="header-section-number">3.2.2.2</span> 多变量列名</h4>
<p>该案列设计比较复杂的正则表达式,<code>new_?(.*)_(.)(.*)</code>需要一定正则表达式基础。
<code>new_?</code>表示匹配<code>new</code>或<code>new_</code>，<code>(.*)</code>匹配任意0次或多次任意字符。</p>
<p><a href="https://www.runoob.com/regexp/regexp-syntax.html">正则表达式介绍</a></p>
<div class="sourceCode" id="cb205"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb205-1"><a href="pivoting.html#cb205-1" aria-hidden="true" tabindex="-1"></a>who <span class="sc">%&gt;%</span> <span class="fu">pivot_longer</span>(</span>
<span id="cb205-2"><a href="pivoting.html#cb205-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">cols =</span> new_sp_m014<span class="sc">:</span>newrel_f65,</span>
<span id="cb205-3"><a href="pivoting.html#cb205-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_to =</span> <span class="fu">c</span>(<span class="st">&quot;diagnosis&quot;</span>, <span class="st">&quot;gender&quot;</span>, <span class="st">&quot;age&quot;</span>), </span>
<span id="cb205-4"><a href="pivoting.html#cb205-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_pattern =</span> <span class="st">&quot;new_?(.*)_(.)(.*)&quot;</span>,</span>
<span id="cb205-5"><a href="pivoting.html#cb205-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">values_to =</span> <span class="st">&quot;count&quot;</span></span>
<span id="cb205-6"><a href="pivoting.html#cb205-6" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb205-7"><a href="pivoting.html#cb205-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 405,440 x 8</span></span>
<span id="cb205-8"><a href="pivoting.html#cb205-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   country     iso2  iso3   year diagnosis gender age   count</span></span>
<span id="cb205-9"><a href="pivoting.html#cb205-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;       &lt;chr&gt; &lt;chr&gt; &lt;int&gt; &lt;chr&gt;     &lt;chr&gt;  &lt;chr&gt; &lt;int&gt;</span></span>
<span id="cb205-10"><a href="pivoting.html#cb205-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 Afghanistan AF    AFG    1980 sp        m      014      NA</span></span>
<span id="cb205-11"><a href="pivoting.html#cb205-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 Afghanistan AF    AFG    1980 sp        m      1524     NA</span></span>
<span id="cb205-12"><a href="pivoting.html#cb205-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 Afghanistan AF    AFG    1980 sp        m      2534     NA</span></span>
<span id="cb205-13"><a href="pivoting.html#cb205-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 Afghanistan AF    AFG    1980 sp        m      3544     NA</span></span>
<span id="cb205-14"><a href="pivoting.html#cb205-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 Afghanistan AF    AFG    1980 sp        m      4554     NA</span></span>
<span id="cb205-15"><a href="pivoting.html#cb205-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 Afghanistan AF    AFG    1980 sp        m      5564     NA</span></span>
<span id="cb205-16"><a href="pivoting.html#cb205-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 405,434 more rows</span></span></code></pre></div>
<p>进一步处理列<code>gender</code>，<code>age</code> 。</p>
<div class="sourceCode" id="cb206"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb206-1"><a href="pivoting.html#cb206-1" aria-hidden="true" tabindex="-1"></a>who <span class="sc">%&gt;%</span> <span class="fu">pivot_longer</span>(</span>
<span id="cb206-2"><a href="pivoting.html#cb206-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">cols =</span> new_sp_m014<span class="sc">:</span>newrel_f65,</span>
<span id="cb206-3"><a href="pivoting.html#cb206-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_to =</span> <span class="fu">c</span>(<span class="st">&quot;diagnosis&quot;</span>, <span class="st">&quot;gender&quot;</span>, <span class="st">&quot;age&quot;</span>), </span>
<span id="cb206-4"><a href="pivoting.html#cb206-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_pattern =</span> <span class="st">&quot;new_?(.*)_(.)(.*)&quot;</span>,</span>
<span id="cb206-5"><a href="pivoting.html#cb206-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_transform =</span> <span class="fu">list</span>(</span>
<span id="cb206-6"><a href="pivoting.html#cb206-6" aria-hidden="true" tabindex="-1"></a>    <span class="at">gender =</span> <span class="sc">~</span> readr<span class="sc">::</span><span class="fu">parse_factor</span>(.x, <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">&quot;f&quot;</span>, <span class="st">&quot;m&quot;</span>)),</span>
<span id="cb206-7"><a href="pivoting.html#cb206-7" aria-hidden="true" tabindex="-1"></a>    <span class="at">age =</span> <span class="sc">~</span> readr<span class="sc">::</span><span class="fu">parse_factor</span>(</span>
<span id="cb206-8"><a href="pivoting.html#cb206-8" aria-hidden="true" tabindex="-1"></a>      .x,</span>
<span id="cb206-9"><a href="pivoting.html#cb206-9" aria-hidden="true" tabindex="-1"></a>      <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">&quot;014&quot;</span>, <span class="st">&quot;1524&quot;</span>, <span class="st">&quot;2534&quot;</span>, <span class="st">&quot;3544&quot;</span>, <span class="st">&quot;4554&quot;</span>, <span class="st">&quot;5564&quot;</span>, <span class="st">&quot;65&quot;</span>), </span>
<span id="cb206-10"><a href="pivoting.html#cb206-10" aria-hidden="true" tabindex="-1"></a>      <span class="at">ordered =</span> <span class="cn">TRUE</span></span>
<span id="cb206-11"><a href="pivoting.html#cb206-11" aria-hidden="true" tabindex="-1"></a>    )</span>
<span id="cb206-12"><a href="pivoting.html#cb206-12" aria-hidden="true" tabindex="-1"></a>  ),</span>
<span id="cb206-13"><a href="pivoting.html#cb206-13" aria-hidden="true" tabindex="-1"></a>  <span class="at">values_to =</span> <span class="st">&quot;count&quot;</span>,</span>
<span id="cb206-14"><a href="pivoting.html#cb206-14" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb206-15"><a href="pivoting.html#cb206-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 405,440 x 8</span></span>
<span id="cb206-16"><a href="pivoting.html#cb206-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   country     iso2  iso3   year diagnosis gender age   count</span></span>
<span id="cb206-17"><a href="pivoting.html#cb206-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;       &lt;chr&gt; &lt;chr&gt; &lt;int&gt; &lt;chr&gt;     &lt;fct&gt;  &lt;ord&gt; &lt;int&gt;</span></span>
<span id="cb206-18"><a href="pivoting.html#cb206-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 Afghanistan AF    AFG    1980 sp        m      014      NA</span></span>
<span id="cb206-19"><a href="pivoting.html#cb206-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 Afghanistan AF    AFG    1980 sp        m      1524     NA</span></span>
<span id="cb206-20"><a href="pivoting.html#cb206-20" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 Afghanistan AF    AFG    1980 sp        m      2534     NA</span></span>
<span id="cb206-21"><a href="pivoting.html#cb206-21" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 Afghanistan AF    AFG    1980 sp        m      3544     NA</span></span>
<span id="cb206-22"><a href="pivoting.html#cb206-22" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 Afghanistan AF    AFG    1980 sp        m      4554     NA</span></span>
<span id="cb206-23"><a href="pivoting.html#cb206-23" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 Afghanistan AF    AFG    1980 sp        m      5564     NA</span></span>
<span id="cb206-24"><a href="pivoting.html#cb206-24" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 405,434 more rows</span></span></code></pre></div>
</div>
<div id="一行多观测值" class="section level4" number="3.2.2.3">
<h4><span class="header-section-number">3.2.2.3</span> 一行多观测值</h4>
<div class="sourceCode" id="cb207"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb207-1"><a href="pivoting.html#cb207-1" aria-hidden="true" tabindex="-1"></a>family <span class="ot">&lt;-</span> <span class="fu">tribble</span>(</span>
<span id="cb207-2"><a href="pivoting.html#cb207-2" aria-hidden="true" tabindex="-1"></a>  <span class="sc">~</span>family, <span class="sc">~</span>dob_child1, <span class="sc">~</span>dob_child2, <span class="sc">~</span>gender_child1, <span class="sc">~</span>gender_child2,</span>
<span id="cb207-3"><a href="pivoting.html#cb207-3" aria-hidden="true" tabindex="-1"></a>  1L, <span class="st">&quot;1998-11-26&quot;</span>, <span class="st">&quot;2000-01-29&quot;</span>, 1L, 2L,</span>
<span id="cb207-4"><a href="pivoting.html#cb207-4" aria-hidden="true" tabindex="-1"></a>  2L, <span class="st">&quot;1996-06-22&quot;</span>, <span class="cn">NA</span>, 2L, <span class="cn">NA</span>,</span>
<span id="cb207-5"><a href="pivoting.html#cb207-5" aria-hidden="true" tabindex="-1"></a>  3L, <span class="st">&quot;2002-07-11&quot;</span>, <span class="st">&quot;2004-04-05&quot;</span>, 2L, 2L,</span>
<span id="cb207-6"><a href="pivoting.html#cb207-6" aria-hidden="true" tabindex="-1"></a>  4L, <span class="st">&quot;2004-10-10&quot;</span>, <span class="st">&quot;2009-08-27&quot;</span>, 1L, 1L,</span>
<span id="cb207-7"><a href="pivoting.html#cb207-7" aria-hidden="true" tabindex="-1"></a>  5L, <span class="st">&quot;2000-12-05&quot;</span>, <span class="st">&quot;2005-02-28&quot;</span>, 2L, 1L,</span>
<span id="cb207-8"><a href="pivoting.html#cb207-8" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb207-9"><a href="pivoting.html#cb207-9" aria-hidden="true" tabindex="-1"></a>family <span class="ot">&lt;-</span> family <span class="sc">%&gt;%</span> <span class="fu">mutate_at</span>(<span class="fu">vars</span>(<span class="fu">starts_with</span>(<span class="st">&quot;dob&quot;</span>)), parse_date)</span>
<span id="cb207-10"><a href="pivoting.html#cb207-10" aria-hidden="true" tabindex="-1"></a>family</span>
<span id="cb207-11"><a href="pivoting.html#cb207-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 5 x 5</span></span>
<span id="cb207-12"><a href="pivoting.html#cb207-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   family dob_child1 dob_child2 gender_child1 gender_child2</span></span>
<span id="cb207-13"><a href="pivoting.html#cb207-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;    &lt;int&gt; &lt;date&gt;     &lt;date&gt;             &lt;int&gt;         &lt;int&gt;</span></span>
<span id="cb207-14"><a href="pivoting.html#cb207-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1      1 1998-11-26 2000-01-29             1             2</span></span>
<span id="cb207-15"><a href="pivoting.html#cb207-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2      2 1996-06-22 NA                     2            NA</span></span>
<span id="cb207-16"><a href="pivoting.html#cb207-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3      3 2002-07-11 2004-04-05             2             2</span></span>
<span id="cb207-17"><a href="pivoting.html#cb207-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4      4 2004-10-10 2009-08-27             1             1</span></span>
<span id="cb207-18"><a href="pivoting.html#cb207-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5      5 2000-12-05 2005-02-28             2             1</span></span></code></pre></div>
<div class="sourceCode" id="cb208"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb208-1"><a href="pivoting.html#cb208-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb208-2"><a href="pivoting.html#cb208-2" aria-hidden="true" tabindex="-1"></a>family <span class="sc">%&gt;%</span> </span>
<span id="cb208-3"><a href="pivoting.html#cb208-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">pivot_longer</span>(</span>
<span id="cb208-4"><a href="pivoting.html#cb208-4" aria-hidden="true" tabindex="-1"></a>    <span class="sc">!</span>family, </span>
<span id="cb208-5"><a href="pivoting.html#cb208-5" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_to =</span> <span class="fu">c</span>(<span class="st">&quot;.value&quot;</span>, <span class="st">&quot;child&quot;</span>), </span>
<span id="cb208-6"><a href="pivoting.html#cb208-6" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_sep =</span> <span class="st">&quot;_&quot;</span>, </span>
<span id="cb208-7"><a href="pivoting.html#cb208-7" aria-hidden="true" tabindex="-1"></a>    <span class="at">values_drop_na =</span> <span class="cn">TRUE</span></span>
<span id="cb208-8"><a href="pivoting.html#cb208-8" aria-hidden="true" tabindex="-1"></a>  )</span>
<span id="cb208-9"><a href="pivoting.html#cb208-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 9 x 4</span></span>
<span id="cb208-10"><a href="pivoting.html#cb208-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   family child  dob        gender</span></span>
<span id="cb208-11"><a href="pivoting.html#cb208-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;    &lt;int&gt; &lt;chr&gt;  &lt;date&gt;      &lt;int&gt;</span></span>
<span id="cb208-12"><a href="pivoting.html#cb208-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1      1 child1 1998-11-26      1</span></span>
<span id="cb208-13"><a href="pivoting.html#cb208-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2      1 child2 2000-01-29      2</span></span>
<span id="cb208-14"><a href="pivoting.html#cb208-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3      2 child1 1996-06-22      2</span></span>
<span id="cb208-15"><a href="pivoting.html#cb208-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4      3 child1 2002-07-11      2</span></span>
<span id="cb208-16"><a href="pivoting.html#cb208-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5      3 child2 2004-04-05      2</span></span>
<span id="cb208-17"><a href="pivoting.html#cb208-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6      4 child1 2004-10-10      1</span></span>
<span id="cb208-18"><a href="pivoting.html#cb208-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 3 more rows</span></span></code></pre></div>
<div class="sourceCode" id="cb209"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb209-1"><a href="pivoting.html#cb209-1" aria-hidden="true" tabindex="-1"></a>anscombe <span class="sc">%&gt;%</span> </span>
<span id="cb209-2"><a href="pivoting.html#cb209-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">pivot_longer</span>(<span class="fu">everything</span>(), </span>
<span id="cb209-3"><a href="pivoting.html#cb209-3" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_to =</span> <span class="fu">c</span>(<span class="st">&quot;.value&quot;</span>, <span class="st">&quot;set&quot;</span>), </span>
<span id="cb209-4"><a href="pivoting.html#cb209-4" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_pattern =</span> <span class="st">&quot;(.)(.)&quot;</span></span>
<span id="cb209-5"><a href="pivoting.html#cb209-5" aria-hidden="true" tabindex="-1"></a>  ) <span class="sc">%&gt;%</span> </span>
<span id="cb209-6"><a href="pivoting.html#cb209-6" aria-hidden="true" tabindex="-1"></a>  <span class="fu">arrange</span>(set)</span>
<span id="cb209-7"><a href="pivoting.html#cb209-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 44 x 3</span></span>
<span id="cb209-8"><a href="pivoting.html#cb209-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   set       x     y</span></span>
<span id="cb209-9"><a href="pivoting.html#cb209-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;</span></span>
<span id="cb209-10"><a href="pivoting.html#cb209-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 1        10  8.04</span></span>
<span id="cb209-11"><a href="pivoting.html#cb209-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 1         8  6.95</span></span>
<span id="cb209-12"><a href="pivoting.html#cb209-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 1        13  7.58</span></span>
<span id="cb209-13"><a href="pivoting.html#cb209-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 1         9  8.81</span></span>
<span id="cb209-14"><a href="pivoting.html#cb209-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 1        11  8.33</span></span>
<span id="cb209-15"><a href="pivoting.html#cb209-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 1        14  9.96</span></span>
<span id="cb209-16"><a href="pivoting.html#cb209-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 38 more rows</span></span></code></pre></div>
<div class="sourceCode" id="cb210"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb210-1"><a href="pivoting.html#cb210-1" aria-hidden="true" tabindex="-1"></a>pnl <span class="ot">&lt;-</span> <span class="fu">tibble</span>(</span>
<span id="cb210-2"><a href="pivoting.html#cb210-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">x =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>,</span>
<span id="cb210-3"><a href="pivoting.html#cb210-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">a =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>,<span class="dv">0</span>, <span class="dv">0</span>),</span>
<span id="cb210-4"><a href="pivoting.html#cb210-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">b =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">1</span>),</span>
<span id="cb210-5"><a href="pivoting.html#cb210-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">y1 =</span> <span class="fu">rnorm</span>(<span class="dv">4</span>),</span>
<span id="cb210-6"><a href="pivoting.html#cb210-6" aria-hidden="true" tabindex="-1"></a>  <span class="at">y2 =</span> <span class="fu">rnorm</span>(<span class="dv">4</span>),</span>
<span id="cb210-7"><a href="pivoting.html#cb210-7" aria-hidden="true" tabindex="-1"></a>  <span class="at">z1 =</span> <span class="fu">rep</span>(<span class="dv">3</span>, <span class="dv">4</span>),</span>
<span id="cb210-8"><a href="pivoting.html#cb210-8" aria-hidden="true" tabindex="-1"></a>  <span class="at">z2 =</span> <span class="fu">rep</span>(<span class="sc">-</span><span class="dv">2</span>, <span class="dv">4</span>),</span>
<span id="cb210-9"><a href="pivoting.html#cb210-9" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb210-10"><a href="pivoting.html#cb210-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb210-11"><a href="pivoting.html#cb210-11" aria-hidden="true" tabindex="-1"></a>pnl <span class="sc">%&gt;%</span> </span>
<span id="cb210-12"><a href="pivoting.html#cb210-12" aria-hidden="true" tabindex="-1"></a>  <span class="fu">pivot_longer</span>(</span>
<span id="cb210-13"><a href="pivoting.html#cb210-13" aria-hidden="true" tabindex="-1"></a>    <span class="sc">!</span><span class="fu">c</span>(x, a, b), </span>
<span id="cb210-14"><a href="pivoting.html#cb210-14" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_to =</span> <span class="fu">c</span>(<span class="st">&quot;.value&quot;</span>, <span class="st">&quot;time&quot;</span>), </span>
<span id="cb210-15"><a href="pivoting.html#cb210-15" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_pattern =</span> <span class="st">&quot;(.)(.)&quot;</span></span>
<span id="cb210-16"><a href="pivoting.html#cb210-16" aria-hidden="true" tabindex="-1"></a>  )</span>
<span id="cb210-17"><a href="pivoting.html#cb210-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 8 x 6</span></span>
<span id="cb210-18"><a href="pivoting.html#cb210-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;       x     a     b time       y     z</span></span>
<span id="cb210-19"><a href="pivoting.html#cb210-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt;  &lt;dbl&gt; &lt;dbl&gt;</span></span>
<span id="cb210-20"><a href="pivoting.html#cb210-20" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1     1     1     0 1     -1.40      3</span></span>
<span id="cb210-21"><a href="pivoting.html#cb210-21" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2     1     1     0 2      0.622    -2</span></span>
<span id="cb210-22"><a href="pivoting.html#cb210-22" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3     2     1     1 1      0.255     3</span></span>
<span id="cb210-23"><a href="pivoting.html#cb210-23" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4     2     1     1 2      1.15     -2</span></span>
<span id="cb210-24"><a href="pivoting.html#cb210-24" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5     3     0     1 1     -2.44      3</span></span>
<span id="cb210-25"><a href="pivoting.html#cb210-25" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6     3     0     1 2     -1.82     -2</span></span>
<span id="cb210-26"><a href="pivoting.html#cb210-26" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 2 more rows</span></span></code></pre></div>
</div>
<div id="重复列名" class="section level4" number="3.2.2.4">
<h4><span class="header-section-number">3.2.2.4</span> 重复列名</h4>
<div class="sourceCode" id="cb211"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb211-1"><a href="pivoting.html#cb211-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">id =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>, <span class="at">y =</span> <span class="dv">4</span><span class="sc">:</span><span class="dv">6</span>, <span class="at">y =</span> <span class="dv">5</span><span class="sc">:</span><span class="dv">7</span>, <span class="at">y =</span> <span class="dv">7</span><span class="sc">:</span><span class="dv">9</span>, <span class="at">.name_repair =</span> <span class="st">&quot;minimal&quot;</span>)</span>
<span id="cb211-2"><a href="pivoting.html#cb211-2" aria-hidden="true" tabindex="-1"></a>df <span class="sc">%&gt;%</span> <span class="fu">pivot_longer</span>(<span class="sc">!</span>id, <span class="at">names_to =</span> <span class="st">&quot;name&quot;</span>, <span class="at">values_to =</span> <span class="st">&quot;value&quot;</span>)</span>
<span id="cb211-3"><a href="pivoting.html#cb211-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 9 x 3</span></span>
<span id="cb211-4"><a href="pivoting.html#cb211-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;      id name  value</span></span>
<span id="cb211-5"><a href="pivoting.html#cb211-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;int&gt; &lt;chr&gt; &lt;int&gt;</span></span>
<span id="cb211-6"><a href="pivoting.html#cb211-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1     1 y         4</span></span>
<span id="cb211-7"><a href="pivoting.html#cb211-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2     1 y         5</span></span>
<span id="cb211-8"><a href="pivoting.html#cb211-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3     1 y         7</span></span>
<span id="cb211-9"><a href="pivoting.html#cb211-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4     2 y         5</span></span>
<span id="cb211-10"><a href="pivoting.html#cb211-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5     2 y         6</span></span>
<span id="cb211-11"><a href="pivoting.html#cb211-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6     2 y         8</span></span>
<span id="cb211-12"><a href="pivoting.html#cb211-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 3 more rows</span></span></code></pre></div>
</div>
</div>
<div id="长转宽" class="section level3" number="3.2.3">
<h3><span class="header-section-number">3.2.3</span> 长转宽</h3>
<p><code>pivot_wider()</code>功能与<code>pivot_longer()</code>相反。通过增加列数减少行数使数据集变得更宽，通常我们在汇总时候使用，达到类似Excel透视表结果。</p>
</div>
<div id="用法-2" class="section level3" number="3.2.4">
<h3><span class="header-section-number">3.2.4</span> 用法</h3>
<div class="sourceCode" id="cb212"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb212-1"><a href="pivoting.html#cb212-1" aria-hidden="true" tabindex="-1"></a>fish_encounters <span class="sc">%&gt;%</span> <span class="fu">pivot_wider</span>(<span class="at">names_from =</span> station, <span class="at">values_from =</span> seen)</span>
<span id="cb212-2"><a href="pivoting.html#cb212-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 19 x 12</span></span>
<span id="cb212-3"><a href="pivoting.html#cb212-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   fish  Release I80_1 Lisbon  Rstr Base_TD   BCE   BCW  BCE2  BCW2   MAE   MAW</span></span>
<span id="cb212-4"><a href="pivoting.html#cb212-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;fct&gt;   &lt;int&gt; &lt;int&gt;  &lt;int&gt; &lt;int&gt;   &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb212-5"><a href="pivoting.html#cb212-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 4842        1     1      1     1       1     1     1     1     1     1     1</span></span>
<span id="cb212-6"><a href="pivoting.html#cb212-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 4843        1     1      1     1       1     1     1     1     1     1     1</span></span>
<span id="cb212-7"><a href="pivoting.html#cb212-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 4844        1     1      1     1       1     1     1     1     1     1     1</span></span>
<span id="cb212-8"><a href="pivoting.html#cb212-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 4845        1     1      1     1       1    NA    NA    NA    NA    NA    NA</span></span>
<span id="cb212-9"><a href="pivoting.html#cb212-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 4847        1     1      1    NA      NA    NA    NA    NA    NA    NA    NA</span></span>
<span id="cb212-10"><a href="pivoting.html#cb212-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 4848        1     1      1     1      NA    NA    NA    NA    NA    NA    NA</span></span>
<span id="cb212-11"><a href="pivoting.html#cb212-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 13 more rows</span></span></code></pre></div>
<div id="缺失值填充" class="section level4" number="3.2.4.1">
<h4><span class="header-section-number">3.2.4.1</span> 缺失值填充</h4>
<p>使用values_fill 参数填充缺失值。如下使用 0 填充缺失值：</p>
<div class="sourceCode" id="cb213"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb213-1"><a href="pivoting.html#cb213-1" aria-hidden="true" tabindex="-1"></a>fish_encounters <span class="sc">%&gt;%</span> <span class="fu">pivot_wider</span>(</span>
<span id="cb213-2"><a href="pivoting.html#cb213-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_from =</span> station, </span>
<span id="cb213-3"><a href="pivoting.html#cb213-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">values_from =</span> seen,</span>
<span id="cb213-4"><a href="pivoting.html#cb213-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">values_fill =</span> <span class="dv">0</span></span>
<span id="cb213-5"><a href="pivoting.html#cb213-5" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb213-6"><a href="pivoting.html#cb213-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 19 x 12</span></span>
<span id="cb213-7"><a href="pivoting.html#cb213-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   fish  Release I80_1 Lisbon  Rstr Base_TD   BCE   BCW  BCE2  BCW2   MAE   MAW</span></span>
<span id="cb213-8"><a href="pivoting.html#cb213-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;fct&gt;   &lt;int&gt; &lt;int&gt;  &lt;int&gt; &lt;int&gt;   &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb213-9"><a href="pivoting.html#cb213-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 4842        1     1      1     1       1     1     1     1     1     1     1</span></span>
<span id="cb213-10"><a href="pivoting.html#cb213-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 4843        1     1      1     1       1     1     1     1     1     1     1</span></span>
<span id="cb213-11"><a href="pivoting.html#cb213-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 4844        1     1      1     1       1     1     1     1     1     1     1</span></span>
<span id="cb213-12"><a href="pivoting.html#cb213-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 4845        1     1      1     1       1     0     0     0     0     0     0</span></span>
<span id="cb213-13"><a href="pivoting.html#cb213-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 4847        1     1      1     0       0     0     0     0     0     0     0</span></span>
<span id="cb213-14"><a href="pivoting.html#cb213-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 4848        1     1      1     1       0     0     0     0     0     0     0</span></span>
<span id="cb213-15"><a href="pivoting.html#cb213-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 13 more rows</span></span></code></pre></div>
</div>
<div id="聚合" class="section level4" number="3.2.4.2">
<h4><span class="header-section-number">3.2.4.2</span> 聚合</h4>
<p>当将数据框长转宽时，通过指定<code>values_fn</code>参数，指定聚合方式，如下所示:</p>
<div class="sourceCode" id="cb214"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb214-1"><a href="pivoting.html#cb214-1" aria-hidden="true" tabindex="-1"></a>warpbreaks <span class="ot">&lt;-</span> warpbreaks <span class="sc">%&gt;%</span> <span class="fu">as_tibble</span>() </span>
<span id="cb214-2"><a href="pivoting.html#cb214-2" aria-hidden="true" tabindex="-1"></a>warpbreaks <span class="sc">%&gt;%</span></span>
<span id="cb214-3"><a href="pivoting.html#cb214-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">pivot_wider</span>(</span>
<span id="cb214-4"><a href="pivoting.html#cb214-4" aria-hidden="true" tabindex="-1"></a>    <span class="at">names_from =</span> wool,</span>
<span id="cb214-5"><a href="pivoting.html#cb214-5" aria-hidden="true" tabindex="-1"></a>    <span class="at">values_from =</span> breaks,</span>
<span id="cb214-6"><a href="pivoting.html#cb214-6" aria-hidden="true" tabindex="-1"></a>    <span class="at">values_fn =</span> mean</span>
<span id="cb214-7"><a href="pivoting.html#cb214-7" aria-hidden="true" tabindex="-1"></a>  )</span>
<span id="cb214-8"><a href="pivoting.html#cb214-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 3 x 3</span></span>
<span id="cb214-9"><a href="pivoting.html#cb214-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   tension     A     B</span></span>
<span id="cb214-10"><a href="pivoting.html#cb214-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;fct&gt;   &lt;dbl&gt; &lt;dbl&gt;</span></span>
<span id="cb214-11"><a href="pivoting.html#cb214-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 L        44.6  28.2</span></span>
<span id="cb214-12"><a href="pivoting.html#cb214-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 M        24    28.8</span></span>
<span id="cb214-13"><a href="pivoting.html#cb214-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 H        24.6  18.8</span></span></code></pre></div>
<p>多个聚合方式</p>
<div class="sourceCode" id="cb215"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb215-1"><a href="pivoting.html#cb215-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot">&lt;-</span> tibble<span class="sc">::</span><span class="fu">tribble</span>(</span>
<span id="cb215-2"><a href="pivoting.html#cb215-2" aria-hidden="true" tabindex="-1"></a>  <span class="sc">~</span>name, <span class="sc">~</span>TYPE, <span class="sc">~</span>num, <span class="sc">~</span>NUM,</span>
<span id="cb215-3"><a href="pivoting.html#cb215-3" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;a&quot;</span>,    1L,   1L,   9L,</span>
<span id="cb215-4"><a href="pivoting.html#cb215-4" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;a&quot;</span>,    1L,   2L,  10L,</span>
<span id="cb215-5"><a href="pivoting.html#cb215-5" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;a&quot;</span>,    2L,   3L,  11L,</span>
<span id="cb215-6"><a href="pivoting.html#cb215-6" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;a&quot;</span>,    2L,   4L,  12L,</span>
<span id="cb215-7"><a href="pivoting.html#cb215-7" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;b&quot;</span>,    1L,   5L,  13L,</span>
<span id="cb215-8"><a href="pivoting.html#cb215-8" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;b&quot;</span>,    2L,   6L,  14L,</span>
<span id="cb215-9"><a href="pivoting.html#cb215-9" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;b&quot;</span>,    2L,   7L,  15L,</span>
<span id="cb215-10"><a href="pivoting.html#cb215-10" aria-hidden="true" tabindex="-1"></a>    <span class="st">&quot;b&quot;</span>,    2L,   8L,  16L</span>
<span id="cb215-11"><a href="pivoting.html#cb215-11" aria-hidden="true" tabindex="-1"></a>  )</span>
<span id="cb215-12"><a href="pivoting.html#cb215-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb215-13"><a href="pivoting.html#cb215-13" aria-hidden="true" tabindex="-1"></a>df <span class="sc">%&gt;%</span> <span class="fu">pivot_wider</span>(<span class="at">names_from =</span> <span class="fu">c</span>(TYPE),<span class="at">values_from =</span> <span class="fu">c</span>(num,NUM),<span class="at">values_fn =</span> <span class="fu">list</span>(<span class="at">num =</span> sum ,<span class="at">NUM =</span> mean))</span>
<span id="cb215-14"><a href="pivoting.html#cb215-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 2 x 5</span></span>
<span id="cb215-15"><a href="pivoting.html#cb215-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   name  num_1 num_2 NUM_1 NUM_2</span></span>
<span id="cb215-16"><a href="pivoting.html#cb215-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt; &lt;int&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt;</span></span>
<span id="cb215-17"><a href="pivoting.html#cb215-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 a         3     7   9.5  11.5</span></span>
<span id="cb215-18"><a href="pivoting.html#cb215-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 b         5    21  13    15</span></span></code></pre></div>
</div>
<div id="从多个变量生成新列名" class="section level4" number="3.2.4.3">
<h4><span class="header-section-number">3.2.4.3</span> 从多个变量生成新列名</h4>
<div class="sourceCode" id="cb216"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb216-1"><a href="pivoting.html#cb216-1" aria-hidden="true" tabindex="-1"></a>production <span class="ot">&lt;-</span> <span class="fu">expand_grid</span>(</span>
<span id="cb216-2"><a href="pivoting.html#cb216-2" aria-hidden="true" tabindex="-1"></a>    <span class="at">product =</span> <span class="fu">c</span>(<span class="st">&quot;A&quot;</span>, <span class="st">&quot;B&quot;</span>), </span>
<span id="cb216-3"><a href="pivoting.html#cb216-3" aria-hidden="true" tabindex="-1"></a>    <span class="at">country =</span> <span class="fu">c</span>(<span class="st">&quot;AI&quot;</span>, <span class="st">&quot;EI&quot;</span>), </span>
<span id="cb216-4"><a href="pivoting.html#cb216-4" aria-hidden="true" tabindex="-1"></a>    <span class="at">year =</span> <span class="dv">2000</span><span class="sc">:</span><span class="dv">2014</span></span>
<span id="cb216-5"><a href="pivoting.html#cb216-5" aria-hidden="true" tabindex="-1"></a>  ) <span class="sc">%&gt;%</span></span>
<span id="cb216-6"><a href="pivoting.html#cb216-6" aria-hidden="true" tabindex="-1"></a>  <span class="fu">filter</span>((product <span class="sc">==</span> <span class="st">&quot;A&quot;</span> <span class="sc">&amp;</span> country <span class="sc">==</span> <span class="st">&quot;AI&quot;</span>) <span class="sc">|</span> product <span class="sc">==</span> <span class="st">&quot;B&quot;</span>) <span class="sc">%&gt;%</span> </span>
<span id="cb216-7"><a href="pivoting.html#cb216-7" aria-hidden="true" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">production =</span> <span class="fu">rnorm</span>(<span class="fu">nrow</span>(.)))</span>
<span id="cb216-8"><a href="pivoting.html#cb216-8" aria-hidden="true" tabindex="-1"></a>production</span>
<span id="cb216-9"><a href="pivoting.html#cb216-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 45 x 4</span></span>
<span id="cb216-10"><a href="pivoting.html#cb216-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   product country  year production</span></span>
<span id="cb216-11"><a href="pivoting.html#cb216-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt;   &lt;chr&gt;   &lt;int&gt;      &lt;dbl&gt;</span></span>
<span id="cb216-12"><a href="pivoting.html#cb216-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 A       AI       2000     -0.244</span></span>
<span id="cb216-13"><a href="pivoting.html#cb216-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 A       AI       2001     -0.283</span></span>
<span id="cb216-14"><a href="pivoting.html#cb216-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 A       AI       2002     -0.554</span></span>
<span id="cb216-15"><a href="pivoting.html#cb216-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 A       AI       2003      0.629</span></span>
<span id="cb216-16"><a href="pivoting.html#cb216-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 A       AI       2004      2.07 </span></span>
<span id="cb216-17"><a href="pivoting.html#cb216-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 A       AI       2005     -1.63 </span></span>
<span id="cb216-18"><a href="pivoting.html#cb216-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 39 more rows</span></span></code></pre></div>
<div class="sourceCode" id="cb217"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb217-1"><a href="pivoting.html#cb217-1" aria-hidden="true" tabindex="-1"></a>production <span class="sc">%&gt;%</span> <span class="fu">pivot_wider</span>(</span>
<span id="cb217-2"><a href="pivoting.html#cb217-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_from =</span> <span class="fu">c</span>(product, country), </span>
<span id="cb217-3"><a href="pivoting.html#cb217-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">values_from =</span> production</span>
<span id="cb217-4"><a href="pivoting.html#cb217-4" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb217-5"><a href="pivoting.html#cb217-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 15 x 4</span></span>
<span id="cb217-6"><a href="pivoting.html#cb217-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;    year   A_AI    B_AI    B_EI</span></span>
<span id="cb217-7"><a href="pivoting.html#cb217-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;int&gt;  &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;</span></span>
<span id="cb217-8"><a href="pivoting.html#cb217-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  2000 -0.244  0.738  -0.313 </span></span>
<span id="cb217-9"><a href="pivoting.html#cb217-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  2001 -0.283  1.89    1.07  </span></span>
<span id="cb217-10"><a href="pivoting.html#cb217-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  2002 -0.554 -0.0974  0.0700</span></span>
<span id="cb217-11"><a href="pivoting.html#cb217-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  2003  0.629 -0.936  -0.639 </span></span>
<span id="cb217-12"><a href="pivoting.html#cb217-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  2004  2.07  -0.0160 -0.0500</span></span>
<span id="cb217-13"><a href="pivoting.html#cb217-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6  2005 -1.63  -0.827  -0.251 </span></span>
<span id="cb217-14"><a href="pivoting.html#cb217-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 9 more rows</span></span></code></pre></div>
<p>通过<code>names_sep</code>和<code>names_prefix</code>参数控制新的列名，或通过<code>names_glue</code></p>
<div class="sourceCode" id="cb218"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb218-1"><a href="pivoting.html#cb218-1" aria-hidden="true" tabindex="-1"></a>production <span class="sc">%&gt;%</span> <span class="fu">pivot_wider</span>(</span>
<span id="cb218-2"><a href="pivoting.html#cb218-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_from =</span> <span class="fu">c</span>(product, country), </span>
<span id="cb218-3"><a href="pivoting.html#cb218-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">values_from =</span> production,</span>
<span id="cb218-4"><a href="pivoting.html#cb218-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_sep =</span> <span class="st">&quot;.&quot;</span>,</span>
<span id="cb218-5"><a href="pivoting.html#cb218-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_prefix =</span> <span class="st">&quot;prod.&quot;</span></span>
<span id="cb218-6"><a href="pivoting.html#cb218-6" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb218-7"><a href="pivoting.html#cb218-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 15 x 4</span></span>
<span id="cb218-8"><a href="pivoting.html#cb218-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;    year prod.A.AI prod.B.AI prod.B.EI</span></span>
<span id="cb218-9"><a href="pivoting.html#cb218-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;int&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;</span></span>
<span id="cb218-10"><a href="pivoting.html#cb218-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  2000    -0.244    0.738    -0.313 </span></span>
<span id="cb218-11"><a href="pivoting.html#cb218-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  2001    -0.283    1.89      1.07  </span></span>
<span id="cb218-12"><a href="pivoting.html#cb218-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  2002    -0.554   -0.0974    0.0700</span></span>
<span id="cb218-13"><a href="pivoting.html#cb218-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  2003     0.629   -0.936    -0.639 </span></span>
<span id="cb218-14"><a href="pivoting.html#cb218-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  2004     2.07    -0.0160   -0.0500</span></span>
<span id="cb218-15"><a href="pivoting.html#cb218-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6  2005    -1.63    -0.827    -0.251 </span></span>
<span id="cb218-16"><a href="pivoting.html#cb218-16" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 9 more rows</span></span></code></pre></div>
<div class="sourceCode" id="cb219"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb219-1"><a href="pivoting.html#cb219-1" aria-hidden="true" tabindex="-1"></a>production <span class="sc">%&gt;%</span> <span class="fu">pivot_wider</span>(</span>
<span id="cb219-2"><a href="pivoting.html#cb219-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_from =</span> <span class="fu">c</span>(product, country), </span>
<span id="cb219-3"><a href="pivoting.html#cb219-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">values_from =</span> production,</span>
<span id="cb219-4"><a href="pivoting.html#cb219-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">names_glue =</span> <span class="st">&quot;prod_{product}_{country}&quot;</span></span>
<span id="cb219-5"><a href="pivoting.html#cb219-5" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb219-6"><a href="pivoting.html#cb219-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 15 x 4</span></span>
<span id="cb219-7"><a href="pivoting.html#cb219-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;    year prod_A_AI prod_B_AI prod_B_EI</span></span>
<span id="cb219-8"><a href="pivoting.html#cb219-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;int&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;</span></span>
<span id="cb219-9"><a href="pivoting.html#cb219-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1  2000    -0.244    0.738    -0.313 </span></span>
<span id="cb219-10"><a href="pivoting.html#cb219-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2  2001    -0.283    1.89      1.07  </span></span>
<span id="cb219-11"><a href="pivoting.html#cb219-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3  2002    -0.554   -0.0974    0.0700</span></span>
<span id="cb219-12"><a href="pivoting.html#cb219-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4  2003     0.629   -0.936    -0.639 </span></span>
<span id="cb219-13"><a href="pivoting.html#cb219-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5  2004     2.07    -0.0160   -0.0500</span></span>
<span id="cb219-14"><a href="pivoting.html#cb219-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6  2005    -1.63    -0.827    -0.251 </span></span>
<span id="cb219-15"><a href="pivoting.html#cb219-15" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 9 more rows</span></span></code></pre></div>
</div>
<div id="多值变宽" class="section level4" number="3.2.4.4">
<h4><span class="header-section-number">3.2.4.4</span> 多值变宽</h4>
<div class="sourceCode" id="cb220"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb220-1"><a href="pivoting.html#cb220-1" aria-hidden="true" tabindex="-1"></a>us_rent_income <span class="sc">%&gt;%</span> </span>
<span id="cb220-2"><a href="pivoting.html#cb220-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">pivot_wider</span>(<span class="at">names_from =</span> variable, <span class="at">values_from =</span> <span class="fu">c</span>(estimate, moe))</span>
<span id="cb220-3"><a href="pivoting.html#cb220-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # A tibble: 52 x 6</span></span>
<span id="cb220-4"><a href="pivoting.html#cb220-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   GEOID NAME       estimate_income estimate_rent moe_income moe_rent</span></span>
<span id="cb220-5"><a href="pivoting.html#cb220-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt;   &lt;chr&gt; &lt;chr&gt;                &lt;dbl&gt;         &lt;dbl&gt;      &lt;dbl&gt;    &lt;dbl&gt;</span></span>
<span id="cb220-6"><a href="pivoting.html#cb220-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 01    Alabama              24476           747        136        3</span></span>
<span id="cb220-7"><a href="pivoting.html#cb220-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 02    Alaska               32940          1200        508       13</span></span>
<span id="cb220-8"><a href="pivoting.html#cb220-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 04    Arizona              27517           972        148        4</span></span>
<span id="cb220-9"><a href="pivoting.html#cb220-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 05    Arkansas             23789           709        165        5</span></span>
<span id="cb220-10"><a href="pivoting.html#cb220-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 06    California           29454          1358        109        3</span></span>
<span id="cb220-11"><a href="pivoting.html#cb220-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 08    Colorado             32401          1125        109        5</span></span>
<span id="cb220-12"><a href="pivoting.html#cb220-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; # ... with 46 more rows</span></span></code></pre></div>
</div>
</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="tidyr-useage.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="tidyr-rectangle.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["Data-Handling-in-R.pdf", "Data-Handling-in-R.epub"],
"toc": {
"collapse": "subsection"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
